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

62 lines
1.6 KiB
Python
Executable File

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod: module
:author: `Eric W`
:date: 2016, october
search and print all solutions of a sudoku puzzle described by a string on the command line
"""
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>'.format(sys.argv[0]))
print('where <grid> is a 81 characters string describing a valid sudoku grid')
print('\nExample: with the grid')
print(GRID)
print('$ {:s} 906028003040500001080900040600000070008206900030000005050003060100002080200870500'.format(sys.argv[0]))
exit(1)
if __name__ == '__main__':
import sys
import sudoku_grid, sudoku_solver
if len(sys.argv) != 2:
usage()
else:
strgrid = sys.argv[1]
try:
grid = sudoku_grid.SudokuGrid(strgrid)
except sudoku_grid.SudokuGridError:
usage()
print('Sudoku to solve')
grid.pretty_print()
sols = sudoku_solver.solve(grid)
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('--')
exit(0)