This is a script I wrote for myself to help me in solving Kakuro puzzles. It finds all the digit combinations with given sum, for example:
$ ruby kakuro.rb 37 6 Combinations: [2, 5, 6, 7, 8, 9] [3, 4, 6, 7, 8, 9] In all: [6, 7, 8, 9] Never: [1]
It displays all of the digit combinations, then a list of digits that appear in every combination then a list of digits that never appear (these are very helpful hints when solving).
If you have already determined some of the digits, you can add them as additional parameters:
$ ruby kakuro.rb 20 4 3 6 Combinations: [2, 3, 6, 9] [3, 4, 6, 7] In all: [3, 6] Never: [1, 5, 8]
This lists only those 4-digit combinations with the sum of 20 that contain both 3 and 6, in this case there are only 2 such combinations (without these constraints there are 12).
This script works a little bit differently. You give it a sum and the clues you have already gathered for each cell and it outputs all of the possible combinations for those constraints. I wrote it while simultaneously learning Haskell, so it may not be the most user friendly script ever :)
Currently there is no command line argument support, so you’ll have to edit the code to provide the arguments (to be precise it’s the pk function call at the end of main function).
The pk
function takes two arguments: an Int
(target sum) and a String
, listing the clues as digit sequences separated by spaces. Use underscore if you have no clues for given cell (which ultimately means the same as 123456789
). For example:
pk 22 "2 13 _ 135 7 45"
means that the sum is 22, you have already determined that there is a 2 in first cell, in the second cell there might be either 1 or 3, you have no clue about third cell and so on.
Please note that you don’t specify the number of cells, it is determined by the number of clues.