-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
337 lines (252 loc) · 10.4 KB
/
solution.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
from utils import *
import utils
from collections import defaultdict
row_units = [cross(r, cols) for r in rows]
column_units = [cross(rows, c) for c in cols]
square_units = [cross(rs, cs) for rs in ('ABC','DEF','GHI') for cs in ('123','456','789')]
diagonal_units = [['A1', 'B2', 'C3', 'D4', 'E5', 'F6', 'G7', 'H8', 'I9'],
['A9', 'B8', 'C7', 'D6', 'E5', 'F4', 'G3', 'H2', 'I1']]
unitlist = row_units + column_units + diagonal_units + square_units
units = dict((s, [u for u in unitlist if s in u]) for s in boxes)
peers = dict((s, set(sum(units[s],[]))-set([s])) for s in boxes)
def naked_twins(values):
"""Eliminate values using the naked twins strategy.
Parameters
----------
values(dict)
a dictionary of the form {'box_name': '123456789', ...}
Returns
-------
dict
The values dictionary with the naked twins eliminated from peers
Notes
-----
Your solution can either process all pairs of naked twins from the input once,
or it can continue processing pairs of naked twins until there are no such
pairs remaining -- the project assistant test suite will accept either
convention. However, it will not accept code that does not process all pairs
of naked twins from the original input. (For example, if you start processing
pairs of twins and eliminate another pair of twins before the second pair
is processed then your code will fail the PA test suite.)
The first convention is preferred for consistency with the other strategies,
and because it is simpler (since the reduce_puzzle function already calls this
strategy repeatedly).
"""
all_pairs = []
for key, val in values.items():
if(len(val) == 2):
all_pairs.append(key)
row_twins = defaultdict(list)
col_twins = defaultdict(list)
square_unit_twins = defaultdict(list)
for i in range(len(all_pairs)):
unit = all_pairs[i]
for j in range(i+1, len(all_pairs)):
other_unit = all_pairs[j]
unit_value = values[unit]
other_unit_value = values[other_unit]
if(unit_value == other_unit_value):
if(same_row(unit, other_unit)):
row_number = get_index_number(unit, row_units)
row_twins[row_number].append([unit, other_unit])
elif(same_col(unit, other_unit)):
col_number = get_index_number(unit, column_units)
col_twins[col_number].append([unit, other_unit])
if(same_square_unit(unit, other_unit)):
square_unit_number = get_index_number(unit, square_units)
square_unit_twins[square_unit_number].append([unit, other_unit])
removeTwinValues(row_twins, row_units, values)
removeTwinValues(col_twins, column_units, values)
removeTwinValues(square_unit_twins, square_units, values)
return values
def removeTwinValues(unit_twin_map, unit_arr, values):
for index, twin_sets in unit_twin_map.items():
for twin_set in twin_sets:
twin1 = twin_set[0]
twin2 = twin_set[1]
twin_value = values[twin1]
for unit in unit_arr[index]:
if((unit != twin1) and (unit != twin2)):
unit_value = values[unit]
unit_value = unit_value.replace(twin_value[0], "")
unit_value = unit_value.replace(twin_value[1], "")
values[unit] = unit_value
def same_row(unit, other_unit):
unit_index_number = get_index_number(unit, row_units)
other_unit_index_number = get_index_number(other_unit, row_units)
if(unit_index_number == -1 or other_unit_index_number == -1):
return False
return unit_index_number == other_unit_index_number
def same_col(unit, other_unit):
unit_index_number = get_index_number(unit,column_units)
other_unit_index_number = get_index_number(other_unit,column_units)
if(unit_index_number == -1 or other_unit_index_number == -1):
return False
return unit_index_number == other_unit_index_number
def same_square_unit(unit, other_unit):
unit_index_number = get_index_number(unit,square_units)
other_unit_index_number = get_index_number(other_unit,square_units)
if(unit_index_number == -1 or other_unit_index_number == -1):
return False;
return unit_index_number == other_unit_index_number
def get_index_number(unit, unit_arr):
for index in range(len(unit_arr)):
list_of_units = unit_arr[index]
if unit in list_of_units:
return index
return -1
def eliminate(values):
"""Apply the eliminate strategy to a Sudoku puzzle
The eliminate strategy says that if a box has a value assigned, then none
of the peers of that box can have the same value.
Parameters
----------
values(dict)
a dictionary of the form {'box_name': '123456789', ...}
Returns
-------
dict
The values dictionary with the assigned values eliminated from peers
"""
# TODO: Copy your code from the classroom to complete this function
for key, value in peers.items():
boxPeers = value
boxValue = values.get(key)
if(len(boxValue) == 1):
for peer in boxPeers:
peerValue = values.get(peer)
if(len(peerValue) > 1):
listOfPossibleValues = peerValue.replace(boxValue,"")
values[peer] = listOfPossibleValues
return values
def only_choice(values):
"""Apply the only choice strategy to a Sudoku puzzle
The only choice strategy says that if only one box in a unit allows a certain
digit, then that box must be assigned that digit.
Parameters
----------
values(dict)
a dictionary of the form {'box_name': '123456789', ...}
Returns
-------
dict
The values dictionary with all single-valued boxes assigned
Notes
-----
You should be able to complete this function by copying your code from the classroom
"""
# TODO: Copy your code from the classroom to complete this function
for unit in unitlist:
for val in '123456789':
boxesContainingVal = [box for box in unit if val in values.get(box)]
if(len(boxesContainingVal) == 1):
values[boxesContainingVal[0]] = val
def reduce_puzzle(values):
"""Reduce a Sudoku puzzle by repeatedly applying all constraint strategies
Parameters
----------
values(dict)
a dictionary of the form {'box_name': '123456789', ...}
Returns
-------
dict or False
The values dictionary after continued application of the constraint strategies
no longer produces any changes, or False if the puzzle is unsolvable
"""
stalled = False
while not stalled:
# Check how many boxes have a determined value
solved_values_before = len([box for box in values.keys() if len(values[box]) == 1])
eliminate(values)
only_choice(values)
naked_twins(values)
# Check how many boxes have a determined value, to compare
solved_values_after = len([box for box in values.keys() if len(values[box]) == 1])
# If no new values were added, stop the loop.
stalled = solved_values_before == solved_values_after
# Sanity check, return False if there is a box with zero available values:
if len([box for box in values.keys() if len(values[box]) == 0]):
return False
return values
def search(values):
"""Apply depth first search to solve Sudoku puzzles in order to solve puzzles
that cannot be solved by repeated reduction alone.
Parameters
----------
values(dict)
a dictionary of the form {'box_name': '123456789', ...}
Returns
-------
dict or False
The values dictionary with all boxes assigned or False
Notes
-----
You should be able to complete this function by copying your code from the classroom
and extending it to call the naked twins strategy.
"""
reduce_puzzle_result = reduce_puzzle(values)
if(reduce_puzzle_result == False):
return False
elif(is_puzzle_solved(values)):
return reduce_puzzle_result
boxWithMinPossibleSolutions = findMinUnSolvedBox(values)
boardVersions = getDiffBoardVersions(values, boxWithMinPossibleSolutions)
for board in boardVersions:
search_result = search(board)
if(search_result != False):
return search_result
return False
def is_puzzle_solved(values):
for box, val in values.items():
if((len(val) == 1) and (int(val) >= 1 and int(val) <= 9)):
for peer in peers[box]:
peer_value = values[peer]
if(val in peer_value):
return False
else:
return False
return True
def findMinUnSolvedBox(values):
minNumPossibleSolutions = 10
minBox = ""
for box, val in values.items():
if(len(val) > 1 and len(val) < minNumPossibleSolutions):
minNumPossibleSolutions = len(val)
minBox = box
return minBox
def getDiffBoardVersions(values, boxWithPossibleSolutions):
if(len(boxWithPossibleSolutions) == 0):
return []
boardVersions = []
for val in values.get(boxWithPossibleSolutions):
newBoard = values.copy()
newBoard[boxWithPossibleSolutions] = val
boardVersions.append(newBoard)
return boardVersions
def solve(grid):
"""Find the solution to a Sudoku puzzle using search and constraint propagation
Parameters
----------
grid(string)
a string representing a sudoku grid.
Ex. '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
Returns
-------
dict or False
The dictionary representation of the final sudoku grid or False if no solution exists.
"""
values = grid2values(grid)
values = search(values)
return values
if __name__ == "__main__":
diag_sudoku_grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
display(grid2values(diag_sudoku_grid))
result = solve(diag_sudoku_grid)
display(result)
try:
import PySudoku
PySudoku.play(grid2values(diag_sudoku_grid), result, history)
except SystemExit:
pass
except:
print('We could not visualize your board due to a pygame issue. Not a problem! It is not a requirement.')