% the program assumes that the constraints are in a file called 'constraints.pl' in the folder that gprolog was started from
calc :-	see('constraints.pl'),
	read(Cs),
	seen,
	write(Cs), nl,
	getVars(Cs, X),
	write(X),
	fd_domain(X, 1, 26),
	fd_all_different(X),
	setSums(Cs),
	fd_labeling(X),
	write(X), nl,
	write(Cs), nl.

getVars([], []).
getVars([C|Cs], X) :- getVars(Cs, X1), getVarList(C, X2), insertVars(X2, X1, X).

getVarList(C = N, C) :- write(C), write(' = '), write(N), nl. 

insertVars([], Vars, Vars).
insertVars([X|Xs], Y, Vars) :- insertVars(Xs, Y, Vars1), insertVar(X, Vars1, Vars).

insertVar(X, [], [X]) :- !.
insertVar(X, [Y|Ys], [Y|Vars]) :- X \== Y, !, insertVar(X, Ys, Vars).
insertVar(_, Ys, Ys).


setSums([]).
setSums([C|Cs]) :- setSum(C), setSums(Cs).

setSum(C = N) :- createSum(C, Sum), Sum #= N, write(Sum), write(' #= '), write(N), nl.

createSum([N], N) :- !.
createSum([N|Ns], N + Sum) :- createSum(Ns, Sum).
