Blog 2020/5/26
<- previous | index | next ->
Update: it should be noted there are many boards which this program cannot solve, such as this one.
Two variations: one which uses mutation, and another in which which all functions return copies of modified data structures.
The example board (wikipedia-board.txt) was taken from https://en.wikipedia.org/wiki/Sudoku.
Simple invocation:
$ ./sudoku.py wikipedia-board.txt
534678912
672195348
198342567
859761423
426853791
713924856
961537284
287419635
345286179
Invocation with execution tracing: here, you can see the remaining possibilities of each grid position being reduced after each iteration, until the board is "solved" (i.e. until there is only one possibility for every grid position):
$ ./sudoku.py --verbose wikipedia-board.txt
Initial board state:
5 3 123456789 123456789 7 123456789 123456789 123456789 123456789
6 123456789 123456789 1 9 5 123456789 123456789 123456789
123456789 9 8 123456789 123456789 123456789 123456789 6 123456789
8 123456789 123456789 123456789 6 123456789 123456789 123456789 3
4 123456789 123456789 8 123456789 3 123456789 123456789 1
7 123456789 123456789 123456789 2 123456789 123456789 123456789 6
123456789 6 123456789 123456789 123456789 123456789 2 8 123456789
123456789 123456789 123456789 4 1 9 123456789 123456789 5
123456789 123456789 123456789 123456789 8 123456789 123456789 7 9
Possibilities count: 489
Iterating rows:
5 3 124689 124689 7 124689 124689 124689 124689
6 23478 23478 1 9 5 23478 23478 23478
123457 9 8 123457 123457 123457 123457 6 123457
8 124579 124579 124579 6 124579 124579 124579 3
4 25679 25679 8 25679 3 25679 25679 1
7 134589 134589 134589 2 134589 134589 134589 6
134579 6 134579 134579 134579 134579 2 8 134579
23678 23678 23678 4 1 9 23678 23678 5
123456 123456 123456 123456 8 123456 123456 7 9
Possibilities count: 321
Iterating columns:
5 3 12469 269 7 12468 14689 1249 248
6 2478 2347 1 9 5 3478 234 2478
123 9 8 2357 345 1247 13457 6 247
8 12457 124579 2579 6 1247 14579 12459 3
4 257 25679 8 5 3 5679 259 1
7 1458 13459 359 2 148 134589 13459 6
139 6 134579 3579 345 147 2 8 47
23 278 2367 4 1 9 3678 23 5
123 1245 123456 2356 8 1246 13456 7 9
Possibilities count: 229
Iterating rows:
5 3 124 26 7 2468 1489 1249 248
6 247 247 1 9 5 3478 234 2478
12 9 8 23 34 24 13457 6 247
8 125 1259 79 6 147 4579 2459 3
4 2 269 8 5 3 79 29 1
7 15 135 9 2 14 458 45 6
139 6 1359 35 35 7 2 8 4
2 278 27 4 1 9 6 3 5
123 1245 12345 2356 8 26 1346 7 9
Possibilities count: 168
<output elided...>
Iterating columns:
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
Possibilities count: 81
Solved in 7 iterations!
534678912
672195348
198342567
859761423
426853791
713924856
961537284
287419635
345286179
Here are some completely unscientific benchmarks:
Interesting that with the higher-end processors and the mutable script, choice of operating-system dominates (the script is so short-lived that this is basically a test of starting a process).
With the lower-end processors, Python 2 vs Python 3 actually makes up to a 2x difference in performance.