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

64 lines
1.8 KiB
Python
Executable File

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod: module
:author: `Éric W`
:date: 2016, october
"""
def usage():
print('Usage: {:s} <bdd> <n>'.format(sys.argv[0]))
print('where')
print('\t<bdd> is a text file whose lines are valid sudoku grids descriptions')
print('\t<n> is the line number to extract in file <bdd>')
print('\nExample: with the grid on line 7 of the file bdd/sudokus.bdd')
print('$ {:s} bdd/sudokus.bdd 7'.format(sys.argv[0]))
exit(1)
if __name__ == '__main__':
import sys
import sudoku_grid, sudoku_solver
if len(sys.argv) != 3:
print('Argument missing.')
usage()
else:
filename = sys.argv[1]
try:
n = int(sys.argv[2])
except ValueError:
print('Second argument must be an integer >=0.')
usage()
try:
with open(filename, 'r') as input:
lines = input.readlines()
except FileNotFoundError:
print('No such file {:s}'.format(filename))
usage()
try:
line = lines[n].rstrip('\n')
except IndexError:
print('File {:s} has no line number {:d}.'.format(filename, n))
usage()
strgrid = line.split(':')[-1]
try:
grid = sudoku_grid.SudokuGrid(strgrid)
except sudoku_grid.SudokuGridError:
print("Line number {:d} of file {:s} has no valid sudoku's grid description".format(n, filename,))
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)