I'm attempting to implement a Skyscraper puzzle solver in Prolog, using constraints (CLPFD).
I've realized a great constraint would be calculating the number of times the maximum switches whilst traversing each row and column and matching it with the side clue.
Here's an example of a single row:
*2* [ _ | _ | _ | _ ] *3* -> *2* [ 1 | 4 | 3 | 2 ] *3*
The list [1, 4, 3, 2]
works for clue 2 because it has 2 maximum switches (0 -> 1 -> 4).
It also works for clue 3 because the same list reversed - [2, 3, 4, 1]
- has 3 maximum switches (0 -> 2 -> 3 -> 4).
I've managed to program a predicate that returns me the number of maximum switches of a list.
The issue is how to actually utilize it to generate a new constraint? I can't pass my list/matrix directly because it isn't yet initialized.
It should probably be something like:
calculate_max_switches(List, Switches),
% Generate a list whose Switches value is equal to the clue number.
Thank you.
permutation/2
. – Tomas By