This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudokuSolver.py
60 lines (46 loc) · 1.61 KB
/
sudokuSolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
import os, sys, string
from operator import itemgetter
from optparse import OptionParser
import io,data,strategy
PUZZLESIZE = 9
def main():
usage = "usage: %prog -f file"
parser = OptionParser(usage)
parser.add_option("-f","--file", dest="file",
help="sudoku puzzle csv file")
(options, args) = parser.parse_args()
# Ensure file given
if (options.file is None):
parser.error("No file given!")
# Check if csv
if not (options.file.endswith('.csv')):
parser.error("Must be csv file!")
# Ensure file exists
if (os.path.exists(os.getcwd()+options.file)):
parser.error("File not found in current directory!")
print '\nI will solve the sudoku in file '+options.file+' asap!\n'
sudokuPuzzle = io.csvToList(options.file)
# Ensure its a standard puzzle
if not (len(sudokuPuzzle)==PUZZLESIZE):
parser.error("Its not a standard "+PUZZLESIZE+"x"+PUZZLESIZE+" puzzle!")
return options.file, sudokuPuzzle
if __name__ == "__main__":
filename, unSolved = main()
io.printPuzzle(unSolved)
# Locate unknowns
unKnowns = data.findZeros(unSolved)
# Get sets of options for each row, col, box
optionSets = data.getSets(unSolved)
# Get cell options
cellOptions = data.getOptions(optionSets, unKnowns)
# Solve the puzzle
unSolved, unKnowns, cellOptions = strategy.implement(unKnowns,cellOptions,unSolved)
if not unKnowns:
io.printPuzzle(unSolved)
print 'The puzzle is solved!\n'
print io.listToCSV(unSolved,os.path.splitext(filename)[0]+'_solved.csv\n')
else:
print '\nI have failed you.\n'
io.printPuzzle(unSolved)
io.printPuzzleAndOptions(unSolved,unKnowns,cellOptions)