:- use_module(library(clpfd)).
:- use_module(library(lists)).

schur(N,P) :-
    statistics(runtime,_),
    length(List,N),   %% List = [X1...Xn], Xi is the part to which i belongs
    domain(List,1,P), %% there are P parts 
    constraints(List, N),
    statistics(runtime,[_,T1]),
    (once((labeling([ff],List),
    statistics(runtime,[_,T2]),
    format("Constraint time: ~q~n",[T1]),
    format("Resolution time: ~q~n",[T2]),
    dump(List,1))) ;
    (once((write('No solution'), nl, 
    statistics(runtime,[_,T2]),
    format("Constraint time: ~q~n",[T1]),
    format("Resolution time: ~q~n",[T2])))
    )). 

constraints(List, N) :-
    recursion(List,1,1,N).


recursion(_,M,_,N):- 
    M > N, !.
recursion(List,I,J,N):-  
    I + J > N,!,
    I1 is I + 1,
    recursion(List,I1,1,N).
recursion(List,I,J,N):-  
    I > J,!,
    J1 is J + 1,
    recursion(List,I,J1,N).
recursion(List,I,J,N) :-
     K is I + J,
    nth(I,List,BlockI),
    nth(J,List,BlockJ),
    nth(K,List,BlockK),
    (BlockI #= BlockJ) #=> (BlockK #\= BlockI),
    J1 is J+1,
    recursion(List,I,J1,N).
    
%%%%%%%%%%%%%%%%%

dump([],_).
dump([A|B],N) :-
    format("~q -> part ~q~n",[N,A]),
    N1 is N+1,
    dump(B,N1).


