I'm trying to write a solver for End View Puzzles, using CLPFD (for those who are unfamiliar, here's a simple description http://www.funwithpuzzles.com/2009/12/abcd-end-view-a1.html ). I'm working on the constraint that I'll apply to each row/column, and running into some trouble.
So I'm thinking it should look something like this:
% NxN board, numbers from 0 to M in the row, Left/Right are the clues
% corresponding to the row
endviews(N,M,List,Left,Right):-
length(List,M),
domain(List,0,M),
stop_repeats(List,M),
left_value(List,M,Left),
reverse(List,RList),
left_value(RList,M,Right)
.
So the first 3 steps are pretty straight forward: initialize the board, set a domain, and make sure that this row won't repeat numbers other than 0
(I've written the stop_repeats/2
predicate). The problem I'm encountering is dealing with the left_value/3
predicate; I'm not really sure how to proceed here, since most of the list is not constrained and I do not actually know the position of this first positive element.
Any help would be greatly appreciated!
I wrote down the puzzle specification. That seems less general than your description of it, then I hardcoded the constraints, using the mapping 0=empty,1=A,2=B,3=C.
test:
I hope you can use it to solve your more general puzzle.
Maybe the
automaton/3
constraint can help here? You can construct a finite automaton that accepts (optional) zero followed by the first letter as indicated, then an arbitrary sequence of zero and letters.