:- use_module(library(clpr)).
:- use_module(library(lists)).

riviere(VitesseRiviere,VitesseVoyageur,Largeur,Distance) :-
    {VitesseRiviere*T = Largeur, VitesseVoyageur*T = Distance}.

relationrecipient(T,A,B,C,A1,B1,C1) :- {T=0,A=A1,B=B1,C=C1}.
relationrecipient(T,A,B,C,A1,B1,C1) :- 
   {A2 = A + 0.04*C - 0.07*A,B2=B+0.07*A+0.03*C-0.05*B,
    C2 = C + 0.05*B - 0.07*C,T > 0,T1=T-1}, 
    relationrecipient(T1,A2,B2,C2,A1,B1,C1).

equilibre(T,I,A,B,C) :- 
       relationrecipient(T,I,I,I,A,B,C),
       relationrecipient(1,A,B,C,A,B,C).

/* ?- equilibre(T,100,A,B,C) */

moyenne([]).
moyenne([_]).
moyenne([_,_]).
    moyenne([X,Y,Z|L]) :- {Y= (X+Z)/2},moyenne([Y,Z|L]).

g1(L) :- L=[0,_,_,_,_,_,_,_,100],moyenne(L).

% Solution Matrice, voir livre de Marriot/Stuckey
rows([_, _]).                                               
rows([R1, R2, R3 | Rs]) :-
    cols(R1, R2, R3), rows([R2, R3 | Rs]).                

cols([_, _], [_, _], [_, _]).                                  
cols([_TL, T, TR | Ts], [ML, M, MR | Ms], [_BL, B, BR | Bs]) :-  
{M = (T + ML + MR + B) / 4},
     cols([T, TR | Ts], [M, MR | Ms], [B, BR | Bs]).

nwrite([]) :- nl.
nwrite([X|L]) :- nwritec(X),nl,nwrite(L).

nwritec([]).
nwritec([X|L]) :- (var(X) -> write(X) ; format("~3g",X)), write(' '), nwritec(L).

% plate goal p192.
gp192(P) :-
    P = [[100,100,100,100,100,100,100],
              [  0,  _,  _,  _,  _,  _,  0],
              [  0,  _,  _,  _,  _,  _,  0],
              [  0,  _,  _,  _,  _,  _,  0],
              [  0,  _,  _,  _,  _,  _,  0],
              [  0,  0,  0,  0,  0,  0,  0]],
    rows(P),
    nwrite(P).

g2(P) :- P = [[_,H,H,H,H,H,H,H,_],
             [G,_,_,_,_,_,_,_,D],
             [G,_,_,_,90,_,_,_,D],
             [G,_,_,_,_,_,_,_,D],
             [G,_,_,_,50,_,90,_,D],
             [G,_,_,_,_,_,_,_,D],
             [G,_,_,_,90,_,_,_,D],
             [G,_,_,_,_,_,_,_,D],
             [_,B,B,B,B,B,B,B,_]], rows(P), nwrite(P).


