I've started writing solvers for games of Simon Tatham's Portable Puzzle Collection.
Those Problems can usually be reduced to some NP-complete problem. This is quite
a fun exercise.
We will be starting with the puzzle "Pattern",
also known as Nonograms that we reduce to SAT.
Unsolved Nonogram
First to explain the puzzle: The cells of the puzzle can be filled with
black or white squares. The numbers on the sides of the rows and colums denote
the number of consecutive black squares. Every run of black squares
has to be seperated from another run by white squares.
The puzzle on the side is defined in the Tatham format by the string
10x10:2/3.2/1.1.3/2.5/2.2.4/1.1.4/1.3/1.1.2/4/4.1/5.2/2.3.3/1.2/6/3/1/4/6/7/4.1.
It has a size information and then the list of runs. Another format we want to
support is the Haskell Runs format which would be
([[2],[3,2],[1,1,3],[2,5],[2,2,4],[1,1,4],[1,3],[1,1,2],[4],[4,1]],[[5,2],[2,3,3],[1,2],[6],[3],[1],[4],[6],[7],[4,1]]).
This format seperates the runs for rows and columns and ommits the size information.
For the second format we can use Haskell's own read function. For Tatham's
format we have to define a parser. I've added type information for most functions
which is often needed for code involving read.
What do we now do with such a problem? First we want to check it for consistency.
If we got size information it should fit the runs and the numbers in the runs
(and the spaces between them) shouldn't add up to more than the size.
But now on to solving the problem. We will reduce this problem to SAT using
the Ersatz library. The encoding of a problem, I guess, is obvious. We say
that every cell within the Nonogram is a Variable Bit and a bit being True
has the semantics of that field being colored black while False would mean
white.
We want to build constraints of single rows or columns and then solve the conjunction
of all those row/column-constraints for satisfiability. Therefore we define a function
of the type [Int] -> [Bit] -> Bit that takes a list of Integers (the numbers of
one column or row) and the list of remaining Bits that do not yet are constrained.
First we begin with the end of the recursion. If no number is left or the remaining
number is zero (for empty columns or rows), we want all remaining variables to be
False. (we take for to be defined as flip map)
The recursion is something to think about a bit. We're given a number of consecutive
black squares x and all the variables in the column/row vars. The x
Trues can be put anywhere from 0 to max_d where max_d is all the rest of
the variables take the minimum necessary space of the remaining number of numbers
xs, while all variables before that number of black squares and one after need
to be white. For every possible position the rest of the variables need to fulfill
the same condition with the rest of the numbers.
Now we have to combine all the constraints from a given problem.
Internally we let Ersatz identify variables by their position in the grid Map (Int,Int) Bit.
Now we can solve our problem easily using minisat with Ersatz's native minisat
binding. Given a solution, we just format it correctly and give it to the user.