% -----------------------------------------
% Exercice 4
% @author "Alexis Fouché" <contact@alexisfouche.net>
% -----------------------------------------

% == Question 1 : Extrait les élements de L
choisir(X, L) :- member(X, L).


% == Question 2 : Permute la liste L en LP
permutation([],[]).
permutation([X|L], LP) :- permutation(L, L1), select(X, LP, L1).


% == Question 3 : Génère les solutions du problème
generer(L) :- L = [N1, N2, N3, N4, N5, N6],
              permutation([1,2,3,4,5,6], [N1, N2, N3, N4, N5, N6]), tester(L).

% == Question 4 : Vérifie si la valeur des nombre sur le triangle est 
%                 une solution du problème
tester([N1, N2, N3, N4, N5, N6]) :- S1 is (N1 + N2 + N3),
                                    S2 is (N3 + N4 + N5),
                                    S3 is (N5 + N6 + N1),
                                    S1=S2,
                                    S2=S3.

% == Question 5 : le prédicat est vérifié si X est un triangle valide
solution(X) :- generer(X), !.

% == Question 6 : calcule le nombre de solutions
nbsolutions(S) :- bagof(X,solution(X),X),
                  length(X,S).