I'm using prolog to try to solve a kakuro puzzle. I have a list of rules like this, where R and C represent the row and starting point of each box, L represents the length and S is the sum of the numbers in the row,
% across(R, C, L, S)
across(2,4,2,4).
across(2,10,2,4).
across(3,4,4,12).
across(3,10,2,6).
across(4,3,2,6).
As far as I can tell, to solve the puzzle across using constraints, for each element L, I would have to find distinct numbers between 1 and 9 which when added up are equal to S. I'm really struggling to work this out, the code I have so far is this:
solveAcross(Solution) :-
findall([R,C,L,S], across(R,C,L,S), List),
Solution = length(List, L),
Solution ins 1..9,
all_distinct(Solution),
labeling([], Solution).
But all this does is return false.
Any help would be appreciated.
So first you need to create your matrix, which will end up being the grid of your kakuro puzzle. That is definitely the first step - apologies if you've already made it.
Then you want to make a few rules for the across constraints:
acrossConstraints(Matrix) :- getAcross(List), applyAcross(Matrix,List).
%this is the overall thing to call, containing two predicates you need to make
getAcross :- findall([R,C,L,S],across(R,C,L,S),List).
%creates a list of the across facts
applyAcross(Matrix,[[R,C,L,S]|T]) :- acrossConstraints(R,C,L,S,M), applyAcross(Matrix,T).
%call another rule and recurse over the list.
acrossConstraints is the tricky bit - here you want to grab the square you want and then make a list starting from that point. Hint: use append and hint.
Good luck!