-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudoku.py
342 lines (300 loc) · 10.5 KB
/
sudoku.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from StringIO import StringIO
import re, traceback, types
import cProfile, pstats, time
import logging
from copy import deepcopy, copy
import puzzles, puzzles2
import models
from models import PVALS, PIDXS, Index, puzzle_range, square_idxs, square
import constraints
logging.basicConfig(level=logging.INFO)
def tryint(v):
try:
return int(v)
except:
return None
class Sudoku (object):
def __init__(self, puzzle, parent=None, depth=1,
start=None, unsolved_idxs=None, possibility_hash=None,
stats=None):
self.stats = stats or models.Stats(puzzle_branches=1,
constraint_steps=0)
self.puzzle = puzzle
self.parent = parent
self.depth = depth
self.unsolved_idxs = unsolved_idxs
self.start = start or time.time()
self.possibility_hash = possibility_hash or self.init_pos_hash()
def init_pos_hash(self):
self.possibility_hash = {}
self.unsolved_idxs = set([])
def _get_pos(idx):
knowns = set()
for i in PIDXS:
knowns.add(self.puzzle[i][idx.col])
for i in PIDXS:
knowns.add(self.puzzle[idx.row][i])
for i, j in square(idx):
knowns.add(self.puzzle[i][j])
knowns.remove(None) # avoids many ifs
return PVALS - knowns
for idx in puzzle_range:
v = self.index_solved(idx)
if v:
pos = set([v])
else:
pos = _get_pos(idx)
self.unsolved_idxs.add(idx)
self.possibility_hash[idx] = pos
return self.possibility_hash
def make_child(self, box=None, new_val=None):
self.stats.puzzle_branches += 1
idx = self.stats.puzzle_branches
if idx % 1000 == 0:
print "Making branch (idx:%d, depth:%d): %s val:%s - %ss" % \
(idx, self.depth, box, new_val, time.time()-self.start)
c = Sudoku(deepcopy(self.puzzle), self, self.depth+1, self.start,
deepcopy(self.unsolved_idxs),
deepcopy(self.possibility_hash),
self.stats)
if box and new_val:
c.set_index_possibilities(box.idx, set([new_val]))
return c
def open_boxes(self):
return sorted([models.Box(idx, self.get_possibilities(idx))
for idx in puzzle_range
if not self.index_solved(idx)],
key=len)
def search(self):
try:
self.constrain()
except models.NoPossibleValues, e:
if self.parent:
raise e
else:
print "ERROR ON BOARD:\n", self.print_help(), "\n\n", self
raise e
if self.is_solved(): return self
logging.debug("Couldn't solve board via constraints, %s\n"
"%s\nStarting to guess", self, self.print_help())
# really only care about the first open box as it WILL be one
# of the values there if our model is correct up till now
# otherwise any mistake is enough to backtrack
box = self.open_boxes()[0]
children = []
for v in box.val or []:
try:
c = self.make_child(box, v)
children.append(c)
sol = c.search()
if sol:
return sol
except models.NoPossibleValues:
pass
def solve(self):
sol = self.search()
if sol and sol.is_solved():
#self.puzzle = deepcopy(sol.puzzle)
sol.status()
else:
print self.print_help(), "\n\n",self
raise Exception("Puzzle Not Solved...")
return sol
def index_solved(self, idx):
return self.puzzle[idx.row][idx.col]
def constrain(self):
# Only resort to a higher reasoning
# when a lesser reasoning system fails us
#
# This should allow us to determine when
# a reasoning system is completely subsumed
# by a more general one (xy_wing vs xy_chain)
def do():
self.stats.inc('constraint_steps')
self._solved_this_cycle = False
self._constrained_this_cycle = False
for con in constraints.constraintsToRun:
con(self)
if self._constrained_this_cycle or self._solved_this_cycle:
return True
while(do()):
pass
def free_in_row(self, idx_in):
return set([idx
for j in PIDXS
for idx in [Index(idx_in.row, j)]
if not self.index_solved(idx)])
def free_in_col(self, idx_in):
return set([idx
for i in PIDXS
for idx in [Index(i, idx_in.col)]
if not self.index_solved(idx)])
def free_in_square(self, idx_in):
return set([idx
for idx in square(idx_in)
if not self.index_solved(idx)])
def free_related_cells(self, idx):
return self.free_in_row(idx)| \
self.free_in_col(idx)|self.free_in_square(idx)
def free_related_possibilities(self, idx):
idxs = self.free_related_cells(idx)-set(idx)
return self.get_possibilities(*idxs)
def closed_square_row(self, idx):
return [i for i in square_idxs(idx.row)
if self.index_solved(Index(i, idx.col))]
def closed_square_col(self, idx):
return [j for j in square_idxs(idx.col)
if self.index_solved(Index(idx.row, j))]
def is_in_row(self, val, row):
for j in PIDXS:
if self.puzzle[row][j] == val:
return True
def is_in_col(self, val, col):
for i in PIDXS:
if self.puzzle[i][col] == val:
return True
def set_index_possibilities(self, idx, pos):
constrained_this_set = False
if len(pos) == 0:
raise models.NoPossibleValues(idx)
old = self.possibility_hash.get(idx, set())
self.possibility_hash[idx] = pos
if len(pos) == 1 and not self.index_solved(idx):
self._solved_this_cycle = True
self.puzzle[idx.row][idx.col] = list(pos)[0]
if idx in self.unsolved_idxs:
self.unsolved_idxs.remove(idx)
for i in self.free_related_cells(idx):
if i == idx:
continue
self.remove_index_possibilities(i, pos)
if old != pos:
self._constrained_this_cycle = True
constrained_this_set = True
return constrained_this_set
def remove_index_possibilities(self, idx,pos):
new_pos = self.get_possibilities(idx)-pos
return self.set_index_possibilities(idx, new_pos)
def get_possibilities(self, *idxs):
if len(idxs) == 0:
return set([])
sets = [self.possibility_hash.get(i) for i in idxs]
return set.union(*sets)
def is_solved(self):
for i in puzzle_range:
if not self.index_solved(i):
return False
return self
def status(self):
s=StringIO()
if self.is_solved():
s.write('Solved Puzzle: \n')
else:
s.write('Unsolved Puzzle:\n')
s.write(str(self.stats))
return s.getvalue()
def __str__(self):
s = StringIO()
s.write("-------------------------------\n")
s.write(self.status())
s.write("-------------------------------\n")
for i in PIDXS:
s.write('|')
for j in PIDXS:
s.write(' ')
if self.index_solved(Index(i, j)):
s.write(self.puzzle[i][j])
else:
s.write('.')
s.write(' ')
if j % 3 == 2:
s.write('|')
s.write('\n')
if i % 3 == 2:
s.write("-------------------------------\n")
return s.getvalue()
def print_help(self):
lb = "-------------------------------"\
"---------------------------------"\
"-------------------------------\n"
s = StringIO()
s.write(lb)
s.write(self.status())
s.write(lb)
for i in PIDXS:
s.write('||')
for j in PIDXS:
idx = models.Index(i, j)
pos = self.get_possibilities(idx)
for l in PVALS:
if l in pos:
s.write(l)
else:
s.write(' ')
s.write('|')
if j % 3 == 2:
s.write('|')
s.write('\n')
if i%3==2: s.write(lb)
return s.getvalue()
def read_puzzle(s):
puzzle = [[None for j in PIDXS]
for i in PIDXS]
# skip first/last line
s = re.sub(r'\n\n+', '\n', re.sub(r'-|\+|\|| |,', "", s))
partial_sol = s.splitlines()[1:]
i, j = 0, 0
for row in partial_sol:
j = 0
if i > 8:
continue
for char in row:
# print i,j,char
if j > 8:
continue
puzzle[i][j] = tryint(char)
j += 1
i += 1
return Sudoku(puzzle)
PUZZLE = read_puzzle(puzzles.puzzles[0])
def solve_puzzle(s):
global PUZZLE
if isinstance(s, str):
s = read_puzzle(s)
PUZZLE = s
p = s.solve()
assert p.is_solved()
PUZZLE = p
return p
def solve_some_puzzles():
i = 1
total_time = 0
puz = puzzles.puzzles # [5:]
stats = models.Stats()
for p in puz:
print "Starting puzzle %s" % i
p = read_puzzle(p)
p.start = time.time()
s = solve_puzzle(p)
stats.inc(s.stats)
print s
ptime = time.time()-p.start
total_time += ptime
print "Done with puzzle %s in %s sec" % (i, ptime)
i += 1
print "\n -- TOTALS -- \nDone with %d puzzles in %s sec:\n%s" % \
(len(puz), total_time, stats)
if __name__ == "__main__":
solve_some_puzzles()
else:
try:
pf = 'sudoku.v4'
# cProfile.run('sudoku.solve_some_puzzles()', pf)
# p = pstats.Stats(pf)
# p.strip_dirs().sort_stats(-1)
# p.sort_stats('time').print_stats(10)
except NameError, e:
print "Reload module to run profiling"
traceback.print_exc();
except Exception, e:
traceback.print_exc();