|
sudokug - Script to solve sudoku-like puzzles. |
sudokug - Script to solve sudoku-like puzzles.
$ sudokug
(front matter displayed here)
sudokug> problem <<eod eod: . . . 4 . . 7 8 9 eod: 4 . 6 . . . 1 . . eod: . 8 . . . . . 5 . eod: 2 . 4 . . 5 . . . eod: . 9 5 . . . . . . eod: . . . 9 . 2 3 4 5 eod: . 3 . . 7 . 9 . 8 eod: . 6 7 . . 1 . . . eod: 9 . . . . 8 . . 2 eod: eod sudokug> solution 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 1 4 3 6 5 8 9 7 3 9 5 8 4 7 2 6 1 6 7 8 9 1 2 3 4 5 5 3 2 6 7 4 9 1 8 8 6 7 2 9 1 5 3 4 9 4 1 5 3 8 6 7 2 sudokug> solution No solution found sudokug> exit
This Perl script is based on the Games::Sudoku::General module. It is capable of solving a variety of Sudoku and Sudoku-like puzzles. In fact, it should be able to solve any puzzle that meets the following criteria:
* The puzzle is based on allocating symbols among cells.
* Each cell contains exactly one symbol.
* A number of sets of cells are specified; each set must contain each symbol exactly once.
* Optionally, some cells may contain initial values.
* Optionally, some cells may be restricted to a subset of all possible symbols.
In theory, any size and topology is possible. What is not possible at the moment is the solution of puzzles requiring logic other than that given above.
There is one command option: -filter, which suppresses the front matter to make the script behave more like a traditional Unix filter. Commands may be piped or redirected in (e.g.
sudokug <commands
or
cat commands | sudokug
) if the user so desires, with or without the -filter option.
Basically, any exposed method in Games::Sudoku::General is a command understood by this script. Arguments to the methods are parameters to the script commands. A few commands have been added to make life for the user of the script easier.
Commands are parsed by Text::ParseWords, so the command name and parameters are whitespace-delimited. Any parameter that contains whitespace must be quoted. Either single (``''') or double ('``') quotes will work.
Because some of the parameters (e.g. specifying a problem) involve a large number of bytes, pseudo-``here documents'' are supported. To specify a ``here document'' as a parameter, specify ``<<string'' in place of the parameter. The script will prompt with the string, and anything you enter is part of that parameter, until you enter a line consisting solely of the given string. More than one ``here document'' can be specified; you will be prompted for them in left-to-right order. There is an example of a ``here document'' in the SYNOPSIS section.
Below is a brief description of the commands. For the attributes that may be set or retrieved, and a more thorough (and possibly more current) discussion of the underlying methods, see the Games::Sudoku::General manpage, or (equivalently) use the command
sudokug> help lib
Cells are numbered from 0 in the order in which they were defined by the topology. Typically this is row order.
sudokug> help lib
it gets you the POD for Games::Sudoku::General.
set() method.
Line breaks may be given (as in the SYNOPSIS), but are treated like any other whitespace.
If you issue this command more than once without an intervening 'problem' command, the solution will be attempted starting where the previous solution left off. If there are multiple solutions to a puzzle, each 'solution' command will get you one, until you run out.
'.' is accepted as a synonym for 'source', but the whitespace before the file name is still required.
F [cell value] - this represents a forced cell. That is, the given value is the only allowed value for the cell. The given cell is set to that value.
N [cell value] - this represents a ``numeration''. That is, the given value can only be supplied by the given cell. The given cell is set to that value.
B [[cell cell ...] value] - ``box claim''. The given value is not possible in the given cells, because they lie outside a set intersection that must contain that value. The given value is eliminated as a possibility for the given cells.
T naked size [[cell cell ...] value] ... - ``naked tuple''. The given value is not possible for the given cell because there exists a ``tuple'' (pair, triple, ...) of cells of the given size which must contain this value, and the given cells are not in the tuple. More than one value can be given, with a list of cells for each. The given value is eliminated as a possibility for the given cells.
T hidden size [[cell cell ...] value] ... - ``hidden tuple''. The given cells are part of a ``tuple'' of cells of the given size that must contain a same-sized ``tuple'' of values, but the given values are not part of the ``tuple'' of values that must be contained in those cells. The given value is eliminated as a possibility for the given cell.
? [cell value] - ``backtrack''. If derivation of the solution reaches a point where none of the above rules can be applied, we simply take a guess at a legal cell value. The cell with the smallest number of possible values is chosen for the guess. If there are more than one such cell, the one with the smallest cell number is chosen. If at any point a solution becomes impossible, we backtrack to the point we took the guess, and try the next possible value.
Any readable attribute may be displayed with the 'get' or 'show' commands, and any writable attribute may be set with the 'set' command. For example:
sudokug> set allowed_symbols <<eod eod: e=2,4,6,8 eod: o=1,3,5,7,9 eod: eod sudokug>
The following simply lists the attributes. Rather than repeat their definitions, you are simply referred to the Games::Sudoku::General manpage, or (equivalently) to the
sudokug> help lib
command.
The last number may be omitted, with the default being the product of the first two. It is in fact deprecated, with the intent of disallowing it in the future.
* a number generates a Dion cube, and specifies the size of the small square. Specifying 3 generates a 9 x 9 x 9 Dion cube.
* 'full' generates a puzzle on all 6 faces of the cube. The sets are the faces of the cube and the ``stripes'' of cells running around the cube in all three directions. The problem is entered face-by-face; if you imagine the cube unfolded into a Latin cross, work top-to-bottom and left-to-right.
* 'half' generates a puzzle on the visible 3 faces of an isometric view of a cube. The sets are halves of the face and the visible ``stripes''. Imagine the visible part of the cube unfolded into the letter ``L'', with the top and right faces divided horizontally, and the remaining face divided vertically. Enter the problem working top-to-bottom and left-to-right.
Caveat: The symbols generated for a 'full' cube are 1 .. 16. The example I have (from http://www.mathrec.org/sudoku/sudokucube.gif) uses 0 through F. If you have one of these, remember to 'set symbols' after you 'set cube full'.
See the Games::Sudoku::General manpage (or 'help lib') for a fuller discussion, with cheesy typed diagrams.
sudokug> set sudoku 3
0.001 T. R. Wyant Initial release 0.003 T. R. Wyant Documented methods and attributes added since 0.001. 0.004 T. R. Wyant - Documented new use of cube attribute. 0.005 T. R. Wyant - Documented new methods and attributes. 0.006 T. R. Wyant Corrected spelling. Allowed input to be redirected or piped in. Added -filter option. Document new methods and attribites
Thomas R. Wyant, III (wyant at cpan dot org)
Copyright 2005, 2006 by Thomas R. Wyant, III (wyant at cpan dot org). All rights reserved.
This module is free software; you can use it, redistribute it and/or modify it under the same terms as Perl itself.
|
sudokug - Script to solve sudoku-like puzzles. |