-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
347 lines (268 loc) · 10.7 KB
/
solver.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
343
344
345
346
347
__author__ = 'Tirth Patel <[email protected]>'
import random
class Cell:
def __init__(self, row, col, number=0):
self.row_coord = row
self.col_coord = col
self.number = number
self.row, self.col, self.box, self.poss = [], [], [], []
def __str__(self):
return str(self.number)
def __repr__(self):
return str('c', str(self.number))
class Sudoku:
def __init__(self, size=9, max_iterations=10000, debug_print=False):
self.size = size
self.sudoku = [[0 for _ in range(size)] for _ in range(size)]
# self.sudoku = [[Cell(i, j) for i in range(size)] for j in range(size)]
self.empties, self.box_empties, self.tried = {}, {}, {}
self.current = 0
self.max_iter = max_iterations
self.empty_spots = len(self.sudoku) ** 2
self.old_sudoku = [[]]
self.reset = False
self.out = debug_print
self.latest = []
def get_input(self, puzzle=None):
if puzzle:
for row in range(len(self.sudoku)):
for cell in range(len(self.sudoku)):
self.sudoku[row][cell] = \
int(puzzle[row * len(self.sudoku) + cell])
else:
print('Enter numbers left to right, row by row, top to bottom, '
'no spaces, 0 for blank: ')
for row in range(len(self.sudoku)):
r = input('Row ' + str(row) + ': ')
for cell in range(len(self.sudoku)):
self.sudoku[row][cell] = int(r[cell])
return self.is_valid(initial=True)
def is_valid(self, initial=False):
rows, cols, boxes = self.get_stuff()
rows_cols_boxes = rows + cols + boxes
if not initial: # check for complete puzzle
for thing in rows_cols_boxes:
if sorted(thing) != [1, 2, 3, 4, 5, 6, 7, 8, 9]:
if self.out:
print('NOPE', thing, rows_cols_boxes.index(thing))
return False
else: # make sure it's valid to start, possible without solution?
self.fill_empties()
for empty in self.empties.keys():
if len(self.empties[empty][3]) == 0:
print('Invalid puzzle')
return False
for thing in rows_cols_boxes:
for number in thing: # numbers not repeated and in range
if number != 0 and not (thing.count(number) == 1
and (1 <= number <= 9)):
print('Invalid puzzle')
return False
return True
def get_stuff(self):
rows, columns, boxes = [], [], []
# for i in range(len(self.sudoku)):
# columns.append([])
# boxes.append([])
columns = [[] for _ in range(self.size)]
boxes = [[] for _ in range(self.size)]
for row in self.sudoku:
rows.append(row)
for i in range(len(row)):
columns[i].append(row[i])
# uggo
for i in range(0, 3):
boxes[0].extend(rows[i][0:3])
boxes[1].extend(rows[i][3:6])
boxes[2].extend(rows[i][6:9])
for i in range(3, 6):
boxes[3].extend(rows[i][0:3])
boxes[4].extend(rows[i][3:6])
boxes[5].extend(rows[i][6:9])
for i in range(6, 9):
boxes[6].extend(rows[i][0:3])
boxes[7].extend(rows[i][3:6])
boxes[8].extend(rows[i][6:9])
return rows, columns, boxes
def __str__(self):
s = ""
cell_counter, row_counter = 0, 0
for row in self.sudoku:
for cell in row:
if cell_counter == 3 or cell_counter == 6:
s += '| '
cell_counter += 1
if cell != 0:
s += str(cell) + ' '
else:
s += ' '
s += '\n'
row_counter += 1
if row_counter == 3 or row_counter == 6:
s += "------|-------|------\n"
cell_counter = 0
s += 'after ' + str(self.current) + ' iterations, ' \
+ str(len(self.empties)) + ' empty spots remain\n'
return s
@staticmethod
def get_box(coordinates):
row = coordinates[0]
col = coordinates[1]
if row < 3:
if col < 3:
box = 0
elif col > 5:
box = 2
else:
box = 1
elif row > 5:
if col < 3:
box = 6
elif col > 5:
box = 8
else:
box = 7
else:
if col < 3:
box = 3
elif col > 5:
box = 5
else:
box = 4
return box
def fill_empties(self):
self.empties = {}
rows, cols, boxes = self.get_stuff()
for row in range(len(self.sudoku)):
for col in range(len(self.sudoku[row])):
if self.sudoku[row][col] == 0: # go through empty cells
# determine box, uggo again
box = self.get_box((row, col))
possibilities = [i for i in range(1, 10) if i not in
(rows[row] + cols[col] + boxes[box])]
# print(row, col)
# print('--')
# print('row', rows[row]) # your boat
# print('col', cols[col])
# print('box', boxes[box])
# print('pos', possibilities)
# print()
self.empties[(row, col)] = \
[rows[row], cols[col], boxes[box], possibilities]
def check_cells(self):
for cell in self.empties.keys():
# only one possibility, put it in the puzzle
if len(self.empties[cell][3]) == 1:
self.sudoku[cell[0]][cell[1]] = self.empties[cell][3].pop()
self.fill_empties()
if self.out:
print(cell, self.sudoku[cell[0]][cell[1]], 'by cells')
def check_boxes(self):
self.box_empties = {}
for cell in self.empties.keys():
# enumerate possibilities per box
box = self.get_box(cell)
if box in self.box_empties.keys():
self.box_empties[box].append(cell)
self.box_empties[box].append(self.empties[cell][3])
else:
self.box_empties[box] = [cell, self.empties[cell][3]]
# each box with empty cells
for vacant in self.box_empties.keys():
current = self.box_empties[vacant] # empty cells in box
counts = {}
# figure out how many times each possibility shows up in box
for possibilities in current:
if type(possibilities) is list: # exclude coordinates
for each in possibilities:
if each in counts.keys():
counts[each] += 1
else:
counts[each] = 1
for key in counts.keys():
if counts[key] == 1: # only showed up once, put in puzzle
for cell in current:
if type(cell) is list and key in cell:
coords = current[current.index(cell) - 1]
self.sudoku[coords[0]][coords[1]] = key
self.fill_empties()
if self.out:
print(coords, key, 'by boxes')
def rando(self):
smallest = 9
for cell in self.empties.keys():
if len(self.empties[cell][3]) < smallest:
smallest = len(self.empties[cell][3])
if smallest == 0: # dead end
# print('')
# print('WHOOPS, DO OVER')
# print('last time', self.latest)
# if self.latest in self.tried.values():
# a = [i for i in self.tried.values()]
# print('tried that', a.count(self.latest), 'times')
# print(self.latest in self.tried.values())
self.tried[self.current] = self.latest
self.sudoku = [row[:] for row in self.old_sudoku]
self.fill_empties()
self.reset = False
self.latest = []
else:
q = [i for i in self.empties.keys()]
sorted(q)
for cell in q:
choices = self.empties[cell][3]
if len(choices) == smallest:
# print(cell, 'CHOICES', choices, '-> ', end='')
choice = choices[random.randrange(smallest)]
# print('PICKED', choice)
self.latest.append([cell, choice])
self.sudoku[cell[0]][cell[1]] = choice
self.fill_empties()
if self.out:
print(cell, self.sudoku[cell[0]][cell[1]], 'by rando')
break
def solve(self): # where the magic happens
if self.out:
print('NOW AT', self.current)
print(self)
if self.empty_spots > len(self.empties) > 0 \
and self.current < self.max_iter:
self.empty_spots = len(self.empties)
self.current += 1
print('--- added ---') if self.out else None
# check by cells
self.check_cells()
# check by boxes
self.check_boxes()
print('-------------\n') if self.out else None
# recurse until solved
return self.solve()
elif len(self.empties) > 0 and self.current < self.max_iter:
self.empty_spots = len(self.empties)
self.current += 1
if not self.reset:
self.old_sudoku = [row[:] for row in self.sudoku]
self.reset = True
print('--- added ---') if self.out else None
# do random stuff
self.rando()
print('--------------\n') if self.out else None
if self.solve():
return True
else:
return False
else:
if self.is_valid():
return True
else:
self.print_empties() if self.out else None
return False
def print_empties(self):
for cell in sorted(self.empties.keys(), key=self.get_box):
print(cell, self.get_box(cell), self.empties[cell][3])
if __name__ == '__main__':
sudoku = Sudoku()
if sudoku.get_input():
if sudoku.solve():
print(sudoku)
print('YAY')