-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
286 lines (230 loc) · 9.04 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
from collections import defaultdict
class Cell(object):
remaining_values = {1, 2, 3, 4, 5, 6, 7, 8, 9}
final_value = None
def __init__(self, row, column, board, initial_value=0):
self.row = row
self.column = column
self.board = board
if initial_value:
self.final_value = initial_value
self.remaining_values = set()
def __repr__(self):
if self.final_value:
return str(self.final_value)
else:
return str(sorted(list(self.remaining_values)))
def _cells_in_row(self):
for cell in self.board[self.row]:
yield cell
def _cells_in_column(self):
for row in self.board:
yield row[self.column]
def _cells_in_square(self):
indices = ((0, 1, 2), (3, 4, 5), (6, 7, 8))
row_indices = indices[self.row / 3]
column_indices = indices[self.column / 3]
for r in row_indices:
for c in column_indices:
yield self.board[r][c]
def process(self):
self._update_possibilities()
self._check_loneliness()
if not self.final_value and len(self.remaining_values) == 1:
self.final_value = self.remaining_values.pop()
def _update_possibilities(self):
if self.final_value:
return
external_final_values = set(
[] # just for formatting, lol
).union(
set([x.final_value for x in self._cells_in_row() if x.final_value])
).union(
set([x.final_value for x in self._cells_in_column() if x.final_value])
).union(
set([x.final_value for x in self._cells_in_square() if x.final_value])
)
self.remaining_values = self.remaining_values - external_final_values
if len(self.remaining_values) == 1:
self.final_value = self.remaining_values.pop()
def _get_possibility_counts(self, cells):
counter = defaultdict(int)
for cell in cells:
for value in cell.remaining_values:
counter[value] += 1
return counter
def _check_loneliness(self):
row_counts = self._get_possibility_counts(self._cells_in_row())
column_counts = self._get_possibility_counts(self._cells_in_column())
square_counts = self._get_possibility_counts(self._cells_in_square())
for remaining_value in self.remaining_values:
if (
(row_counts[remaining_value] == 1) or
(column_counts[remaining_value] == 1) or
(square_counts[remaining_value] == 1)
):
self.final_value = remaining_value
self.remaining_values = set()
return
class Board(object):
def __init__(self, board):
# initialise rows to put cell objects in
self._cell_board = [[], [], [], [], [], [], [], [], []]
for row in xrange(9):
for column in xrange(9):
value = board[row][column]
cell = Cell(row, column, self._cell_board, value)
self[row].append(cell)
@property
def reached_contradiction(self):
return any(
(not cell.final_value and not cell.remaining_values)
for cell in self.cells
)
@property
def solved(self):
return all(cell.final_value for cell in self.cells)
@property
def cells(self):
for row in xrange(9):
for column in xrange(9):
cell = self[row][column]
yield cell
@property
def basic_representation(self):
rows = []
for row in self._cell_board:
rows.append([cell.final_value or 0 for cell in row])
return rows
def __getitem__(self, i):
return self._cell_board[i]
def __repr__(self):
line_format = "{} {} {} | {} {} {} | {} {} {}"
row_separator = "------+-------+------"
number_display = lambda x: x.final_value if x.final_value else ' '
lines = []
lines.append(line_format.format(*map(number_display, self[0][0]._cells_in_row())))
lines.append(line_format.format(*map(number_display, self[1][0]._cells_in_row())))
lines.append(line_format.format(*map(number_display, self[2][0]._cells_in_row())))
lines.append(row_separator)
lines.append(line_format.format(*map(number_display, self[3][0]._cells_in_row())))
lines.append(line_format.format(*map(number_display, self[4][0]._cells_in_row())))
lines.append(line_format.format(*map(number_display, self[5][0]._cells_in_row())))
lines.append(row_separator)
lines.append(line_format.format(*map(number_display, self[6][0]._cells_in_row())))
lines.append(line_format.format(*map(number_display, self[7][0]._cells_in_row())))
lines.append(line_format.format(*map(number_display, self[8][0]._cells_in_row())))
return '\n' + '\n'.join(lines) + '\n'
def find_smallest_guess(self):
# bit of an optimisation - if there's a cell with like 9 things you could guess that,
# that might take a while. So let's just find the cell with the fewest possibilities
# and pick one of those.
row = None
column = None
guesses = None
for cell in self.cells:
if not cell.remaining_values:
continue
if (guesses is None) or len(guesses) > len(cell.remaining_values):
row = cell.row
column = cell.column
guesses = cell.remaining_values
if not guesses:
print "wtf"
raise AssertionError
return row, column, guesses
@property
def is_valid(self):
# well this is an overly-onerous way of doing this, but screw it.
for cell in self.cells:
row_values = set(x.final_value for x in cell._cells_in_row())
column_values = set(x.final_value for x in cell._cells_in_column())
square_values = set(x.final_value for x in cell._cells_in_square())
all_equal = row_values == column_values == square_values == {1, 2, 3, 4, 5, 6, 7, 8, 9}
if not all_equal:
return False
return True
@property
def state(self):
# gives a string representation of the full state of the board
# including assumptions
return str(list(self.cells))
def solve(self, verbose=True, recursion_level=0):
if recursion_level > 5:
raise Exception
while True:
if verbose:
print self
state_0 = self.state
for cell in self.cells:
cell.process()
state_1 = self.state
if state_0 == state_1:
if self.solved:
if self.is_valid:
return self
return False
if self.reached_contradiction:
if verbose:
print "contradiction found."
return False
# stalemate, then - need to make a guess.
row, column, guesses = self.find_smallest_guess()
for guess in guesses:
basic_representation = self.basic_representation
basic_representation[row][column] = guess
new_board = Board(basic_representation)
if verbose:
print "Making assumption: row={row}, col={col}, value={guess}...".format(row=row, col=column, guess=guess)
print self
result = new_board.solve(verbose=verbose, recursion_level=recursion_level+1)
if result:
return result
return False
test_board_1 = [
[7, 5, 0, 0, 3, 8, 0, 0, 0],
[0, 0, 0, 5, 0, 0, 9, 0, 0],
[0, 9, 0, 0, 7, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 8, 2, 1],
[1, 3, 4, 0, 0, 0, 7, 5, 6],
[2, 8, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 6, 0, 2, 0, 0, 3, 0],
[0, 0, 5, 0, 0, 3, 0, 0, 0],
[0, 0, 0, 4, 1, 0, 0, 9, 2],
]
test_board_2 = [
[0, 0, 0, 0, 0, 9, 0, 0, 0],
[0, 0, 8, 0, 6, 0, 0, 0, 0],
[7, 0, 5, 4, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 3, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 5, 0],
[5, 8, 7, 0, 0, 0, 2, 0, 0],
[4, 0, 0, 7, 0, 0, 9, 6, 0],
[9, 0, 0, 2, 0, 0, 0, 0, 0],
[0, 2, 6, 5, 0, 0, 4, 0, 0],
]
test_board_3 = [
[0, 0, 0, 5, 0, 4, 0, 0, 0],
[0, 0, 8, 0, 0, 0, 3, 0, 0],
[7, 5, 0, 0, 2, 0, 0, 0, 6],
[0, 0, 0, 7, 8, 0, 2, 6, 4],
[0, 6, 0, 0, 9, 0, 0, 0, 8],
[5, 0, 0, 0, 0, 6, 0, 0, 0],
[0, 0, 5, 0, 3, 0, 9, 0, 7],
[0, 0, 2, 6, 0, 9, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 3, 0],
]
# The hardest button to button
test_board_x = [
[8, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 3, 6, 0, 0, 0, 0, 0],
[0, 7, 0, 0, 9, 0, 2, 0, 0],
[0, 5, 0, 0, 0, 7, 0, 0, 0],
[0, 0, 0, 0, 4, 5, 7, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 3, 0],
[0, 0, 1, 0, 0, 0, 0, 6, 8],
[0, 0, 8, 5, 0, 0, 0, 1, 0],
[0, 9, 0, 0, 0, 0, 4, 0, 0],
]
#board = Board(test_board_x)
#board.solve()