Using the Standard Solver#
Info
You can find the code of this guide under examples/doc_reference in the source repository. Make sure so set doc_reference as working directory to get the expected behavior.
After having installed the package to your environment, the pre-constructed solver can be easily accessed in a functional way by means of the functions load, solve and save. Begin by importing the respective functions from sudoku:
1 | |
Load#
Use the load function to create and return a Sudoku object from the unsolved puzzle. The puzzle must be stored as csv file whose entries represent the initial configuration of the Sudoku grid. Any unspecified tiles take the value 0 as placeholder. Let's, e.g., consider the puzzle represented by the content of example.csv (the file should be stored in your working directory).
0, 0, 1, 0, 0, 4, 0, 8, 0
0, 0, 5, 0, 0, 0, 0, 0, 7
4, 7, 0, 0, 3, 0, 0, 6, 0
6, 4, 0, 0, 0, 8, 0, 0, 9
1, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 5, 0, 8, 0, 0
0, 0, 0, 2, 0, 0, 0, 3, 0
0, 0, 7, 0, 0, 0, 0, 0, 0
8, 9, 0, 0, 0, 5, 0, 0, 6
Load it into a Sudoku structure by:
2 | |
Solve#
Invoke the solving process by passing the Sudoku object to the solve function. Before returning the solved puzzle, the solver will guide you through the solving process by printing some of the solving steps to the console (including the final result). A solving step, thereby, refers to any operation that lets us exclude one of the remaining candidates of an 'unsolved' tile.
Use the second argument of the solve function to decide what solving steps to print. Refer to the documentation of solve to see what options are available. In this example, we will only print some less trivial steps by passing "interesting" as argument. Moreover, for the purpose of this guide, we're using ASCII characters to format the output by setting the unicode keyword to False.
3 | |
The console output contains the present state of the puzzle as well as information about the conclusions made to remove certain candidates in the shown step. Observe that both the affected candidates (i.e. those we are to remove) and those we considered to draw conclusions are highlighted by means of ANSI escape codes. Rendering thus depends on the console you're using. A typical output (without highlighting the considered candidates) could look like:
solving step 410: tile 16 is the only tile in column 7 with 4 as option
status: ok
+-----------------------+-----------------------+-----------------------+
| [9] [6] [1] | [5] [7] [4] | [3] [8] [2] |
| [3] [8] [5] | [6,9] [6,9] [2] | [1,4] [1,4,9][7] |
| [4] [7] [2] | [8] [3] [1] | [9] [6] [5] |
+-----------------------+-----------------------+-----------------------+
| [6] [4] [3] | [1] [2] [8] | [5,7] [5,7] [9] |
| [1] [5] [8] | [7] [4] [9] | [6] [2] [3] |
| [7] [2] [9] | [6] [5] [3] | [8] [1] [4] |
+-----------------------+-----------------------+-----------------------+
| [5] [1] [6] | [2] [9] [7] | [4] [3] [8] |
| [2] [3] [7] | [4] [8] [6] | [5] [9] [1] |
| [8] [9] [4] | [3] [1] [5] | [2] [7] [6] |
+-----------------------+-----------------------+-----------------------+
next step: (press ENTER)
Eventually, the solved puzzle is rendered to the console once more before the solve function returns.
solving step 415: puzzle solved
status: ok
+-----------+-----------+-----------+
| [9][6][1] | [5][7][4] | [3][8][2] |
| [3][8][5] | [9][6][2] | [1][4][7] |
| [4][7][2] | [8][3][1] | [9][6][5] |
+-----------+-----------+-----------+
| [6][4][3] | [1][2][8] | [7][5][9] |
| [1][5][8] | [7][4][9] | [6][2][3] |
| [7][2][9] | [6][5][3] | [8][1][4] |
+-----------+-----------+-----------+
| [5][1][6] | [2][9][7] | [4][3][8] |
| [2][3][7] | [4][8][6] | [5][9][1] |
| [8][9][4] | [3][1][5] | [2][7][6] |
+-----------+-----------+-----------+
Save#
You may then want to write the solved puzzle to a file named, e.g., solved.csv by using the save function:
4 | |
9,6,1,5,7,4,3,8,2
3,8,5,9,6,2,1,4,7
4,7,2,8,3,1,9,6,5
6,4,3,1,2,8,7,5,9
1,5,8,7,4,9,6,2,3
7,2,9,6,5,3,8,1,4
5,1,6,2,9,7,4,3,8
2,3,7,4,8,6,5,9,1
8,9,4,3,1,5,2,7,6
The whole program can now be compactly written as
1 2 | |
Info
Find a similar example in the source repository under examples/std_examples.