:- use_module(library(clpfd)).

avantdernier([E,_],E).

avantdernier([_|L],E):-
    avantdernier(L,E).

inverse([],[]).
inverse([A|L],L1):-
    inverse(L,L2),
    append(L2,[A],L1).


fizzbuzz(0,[]).

fizzbuzz(A,L):-
    A > 0,
    A mod 3 #=0,
    A mod 5 #=0,
    A1 #= A-1,
    fizzbuzz((A1),L1),
    append(L1,[fizzbuzz],L).

fizzbuzz(A,L):-
    A > 0,
    A mod 3 #=0,
    A mod 5 #\=0,
    A1 #= A-1,
    fizzbuzz((A1),L1),
    append(L1,[fizz],L).

fizzbuzz(A,L):-
    A > 0,
    A mod 3 #\=0,
    A mod 5 #=0,
    A1 #= A-1,
    fizzbuzz((A1),L1),
    append(L1,[buzz],L).

fizzbuzz(A,L):-
    A > 0,
    A mod 3 #\=0,
    A mod 5 #\=0,
    A1 #= A-1,
    fizzbuzz((A1),L1),
    append(L1,[A],L).




arete(1, 2).
arete(2, 3).
arete(3, 4).
arete(4, 1).

nbSommets(NbSommets) :-
    findall(S1, arete(S1,_), L1),
    findall(S2, arete(_, S2), L2),
    append(L1, L2, L),
    sort(L, LL),
    length(LL, NbSommets).

coloration_valide(L, NbCouleurs) :-
    nbSommets(NbSommets),
    length(L, NbSommets),
    L ins 1..NbCouleurs,
    findall((S1, S2), arete(S1, S2), ListeAretes),
    contraintes(L, ListeAretes).

contraintes(_,[]).
contraintes(L, [(S1, S2)|Reste]) :-
    nth1(S1, L, C1),
    nth1(S2, L, C2),
    C1 #\= C2,
    contraintes(L, Reste).


 coloration_minimale(Coloration) :-
    between(1, inf, NbCouleurs),
    coloration_valide(Coloration, NbCouleurs),
    label(Coloration),
    !.

