Sudoku/main4.py
2021-01-07 11:13:30 +01:00

75 lines
2.0 KiB
Python
Executable File

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod: module
:author: `Éric W`
:date: 2016, october
"""
GRID='''
+-------+-------+-------+
| 9 . 6 | . 2 8 | . . 3 |
| . 4 . | 5 . . | . . 1 |
| . 8 . | 9 . . | . 4 . |
+-------+-------+-------+
| 6 . . | . . . | . 7 . |
| . . 8 | 2 . 6 | 9 . . |
| . 3 . | . . . | . . 5 |
+-------+-------+-------+
| . 5 . | . . 3 | . 6 . |
| 1 . . | . . 2 | . 8 . |
| 2 . . | 8 7 . | 5 . . |
+-------+-------+-------+
'''
def usage():
print('Usage: {:s} <grid> <dot_filename>'.format(sys.argv[0]))
print('where\n\t<grid> is a 81 characters string describing a valid sudoku grid')
print('\t<dot_filename> is the name of the file containing the resolution\'s tree description')
print('\nExample: with the grid')
print(GRID)
print('$ {:s} 906028003040500001080900040600000070008206900030000005050003060100002080200870500 tree_resolution'.format(sys.argv[0]))
exit(1)
if __name__ == '__main__':
import sys, os
import sudoku_grid, sudoku_solver
if len(sys.argv) != 3:
usage()
else:
strgrid = sys.argv[1]
try:
grid = sudoku_grid.SudokuGrid(strgrid)
except sudoku_grid.SudokuGridError:
usage()
filename = sys.argv[2]
print('Sudoku to solve')
grid.pretty_print()
with open(filename + '.dot', 'w') as output:
node = 'START'
output.write(
'''/*
Resolution tree for sudoku number
*/
digraph T {{
\tbgcolor="#FFFF00";
\tnode[style=filled];
\t{:s}[shape=hexagon, fillcolor="#FF0000"];
'''.format(node))
sols = sudoku_solver.solve(grid, output, node)
output.write('}\n')
nb_sols = len(sols)
print('Number of solution(s): {:d}'.format(nb_sols))
print('--')
for i in range(nb_sols):
print('Solution {:d}'.format(i + 1))
sols[i].pretty_print()
print('--')
os.system('dot -Tpng -o {0:s}.png {0:s}.dot'.format(filename))
exit(0)