-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudokuer.py
210 lines (197 loc) · 6.18 KB
/
sudokuer.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python
# coding: utf-8
# default background color: #300a24
# default text color: #839496
from collections import namedtuple
Entry = namedtuple('Entry', 'pos val')
ROWS = [range(9*i, 9*(i+1)) for i in range(9)]
COLUMNS = [range(i, i+80, 9) for i in range(9)]
BOXES = []
for i in range(9):
b = []
for l in range((i/3)*3, (i/3)*3+3):
for c in range((i%3)*3, (i%3)*3+3):
b.append(9*l+c)
BOXES.append(b)
def make_sudoku_solver(puzzle):
solver = SudokuSolver()
solver.set_sudoku(puzzle)
if solver.check_sudoku():
return solver
raise Exception("invalid input puzzle")
class SudokuSolver(object):
def __init__(self):
self.puzzle = []
self.stack = []
self.peercache = {}
#self.counter = 0
def set_sudoku(self, puzzle):
self.puzzle = puzzle
def check_sudoku(self):
for pos in range(81):
if self.puzzle[pos] == 0:
continue
else:
v = self.puzzle[pos]
if not self.__check_value(pos, v, initial_check=True):
return False
return True
def __check_value(self, pos, x, initial_check=False):
if pos not in self.peercache:
line = pos / 9
col = pos % 9
box = (line / 3) * 3 + col / 3
row = ROWS[line]
column = COLUMNS[col]
boxbox = BOXES[box]
self.peercache[pos] = set(row + column + boxbox)
for i in self.peercache[pos]:
if initial_check and i == pos:
continue
if self.puzzle[i] == x:
return False
#for i in row:
# if initial_check and i == pos:
# continue
# if self.puzzle[i] == x:
# return False
#for i in column:
# if initial_check and i == pos:
# continue
# if self.puzzle[i] == x:
# return False
#for i in boxbox:
# if initial_check and i == pos:
# continue
# if self.puzzle[i] == x:
# return False
return True
#i = line * 9
#while i < (line+1) * 9:
# if initial_check and i == pos:
# i += 1
# continue
# if self.puzzle[i] != 0 and self.puzzle[i] == x:
# return False
# i += 1
#i = col
#while i <= (col + 72):
# if initial_check and i == pos:
# i += 9
# continue
# if self.puzzle[i] != 0 and self.puzzle[i] == x:
# return False
# i += 9
#topleft = (box / 3) * 27 + (box % 3) * 3
#i = topleft
#while i <= (topleft+20):
# for j in range(3):
# if initial_check and (i+j) == pos:
# continue
# if self.puzzle[i+j] != 0 and self.puzzle[i+j] == x:
# return False
# i += 9
#return True
def make_guess(self, pos, b):
guess = b+1
while guess <= 9:
if self.__check_value(pos, guess):
return guess
guess += 1
return guess
def solve(self):
while 1:
i = 0
while i < 81:
if self.puzzle[i] == 0:
#print '.',
break
i += 1
if i >= 81:
print ("done!")
break
v = self.make_guess(i, 0)
#print ';; v', v
if v <= 9:
self.puzzle[i] = v
self.stack.append(Entry(i, v))
else:
while len(self.stack) > 0:
#print 'stack depth:', len(self.stack)
e = self.stack.pop()
v = self.make_guess(e.pos, e.val)
#print ';; adjust v', v
if v <= 9:
self.puzzle[e.pos] = v
self.stack.append(Entry(e.pos, v))
break
else:
# reset the cell referred by top of the stack
self.puzzle[e.pos] = 0
if len(self.stack) == 0:
raise Exception("bad sudoku, no solution")
def show_solution(self):
lines = []
for i in range(81):
if i % 9 == 0:
l = []
l.append(str(self.puzzle[i]))
elif (i+1) % 9 == 0:
l.append(str(self.puzzle[i]))
lines.append(' '.join(l))
else:
l.append(str(self.puzzle[i]))
print '\n'.join(lines)
if __name__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