Sudoku/main3.py

53 lines
1.3 KiB
Python
Raw Normal View History

2021-01-06 19:10:54 +01:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod: module
:author: `Éric W`
:date: 2016, october
"""
def usage():
print('Usage: {:s} <bdd>'.format(sys.argv[0]))
print('where')
print('\t<bdd> is a text file whose lines are valid sudoku grids descriptions')
exit(1)
if __name__ == '__main__':
import sys
import sudoku_grid, sudoku_solver
if len(sys.argv) != 2:
print('Argument missing.')
usage()
else:
filename = sys.argv[1]
try:
with open(filename, 'r') as input:
lines = input.readlines()
except FileNotFoundError:
print('No such file {:s}'.format(filename))
usage()
with open(filename+'.sol', 'w') as output:
for line in lines:
line = line.rstrip('\n')
output.write(line + ':')
strgrid = line.split(':')[-1]
grid = sudoku_grid.SudokuGrid(strgrid)
sols = sudoku_solver.solve(grid)
nb_sols = len(sols)
if nb_sols == 1:
output.write('y:')
else:
output.write('n:')
sols = ':'.join(str(sol) for sol in sols)
output.write(sols + '\n')
exit(0)