-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
324 lines (295 loc) · 13.3 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
assignments = []
def cross(A, B):
"Cross product of elements in A and elements in B."
return [s+t for s in A for t in B]
pass
rows = 'ABCDEFGHI'
cols = '123456789'
boxes = cross(rows, cols)
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')]
#Added list for diagonal units
diagonal_units = [['A1','B2','C3','D4','E5','F6','G7','H8','I9'],['I1','H2','G3','F4','E5','D6','C7','B8','A9']]
#Added diagonal units to unitlist
unitlist = row_units + column_units + square_units + diagonal_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 assign_value(values, box, value):
"""
Please use this function to update your values dictionary!
Assigns a value to a given box. If it updates the board record it.
"""
# Don't waste memory appending actions that don't actually change any values
if values[box] == value:
return values
values[box] = value
if len(value) == 1:
assignments.append(values.copy())
return values
def naked_twins(values):
"""Eliminate values using the naked twins strategy.
Args:
values(dict): a dictionary of the form {'box_name': '123456789', ...}
Returns:
the values dictionary with the naked twins eliminated from peers.
"""
# Find all instances of naked twins
# Eliminate the naked twins as possibilities for their peers
#Initialise flag and lists
flag = 1 #flag to decide whether to search for naked twins or not
my_list_row = []
my_list_col = []
my_list_sq = []
my_list_diagonal = []
#Start search for naked twins
while flag:
flag = 0
#Search for naked twins in row units
for i in range(9): #scan through all the row units
for index, box in enumerate(row_units[i]):
if len(values[box]) == 2:
if index == 8:
#Prevent out of range error if box is last in the unit
continue
else:
for j in range(index+1,9):
if values[box] == values[row_units[i][j]]:
if box in my_list_row:
break
else:
#Naked twins were found.
#Add boxes to the list of already tested naked twins
my_list_row.append(box)
my_list_row.append(row_units[i][j])
for k in range(9):
if (k != index and k != j) and len(values[row_units[i][k]]) > 1:
flag = 1 #Set search continue flag
for digit in values[box]: #Remove digits in the naked twins from other box in the unit
values[row_units[i][k]] = values[row_units[i][k]].replace(digit,"")
break
#Search for naked twins in column units
for i in range(9): #scan through all the column units
for index, box in enumerate(column_units[i]):
if len(values[box]) == 2:
if index == 8:
#Prevent out of range error if box is last in the unit
continue
else:
for j in range(index+1,9):
if values[box] == values[column_units[i][j]]:
if box in my_list_col:
break
else:
#Naked twins were found.
#Add boxes to the list of already tested naked twins
my_list_col.append(box)
my_list_col.append(column_units[i][j])
for k in range(9):
if (k != index and k != j) and len(values[column_units[i][k]]) > 1:
flag = 1 #Set search continue flag
for digit in values[box]: #Remove digits in the naked twins from other box in the unit
values[column_units[i][k]] = values[column_units[i][k]].replace(digit,"")
break
#Search for naked twins in square units
for i in range(9): #scan through all the square units
for index, box in enumerate(square_units[i]):
if len(values[box]) == 2:
if index == 8:
#Prevent out of range error if box is last in the unit
continue
else:
for j in range(index+1,9):
if values[box] == values[square_units[i][j]]:
if box in my_list_sq:
break
else:
#Naked twins were found.
#Add boxes to the list of already tested naked twins
my_list_sq.append(box)
my_list_sq.append(square_units[i][j])
for k in range(9):
if (k != index and k != j) and len(values[square_units[i][k]]) > 1:
flag = 1 #Set search continue flag
for digit in values[box]: #Remove digits in the naked twins from other box in the unit
values[square_units[i][k]] = values[square_units[i][k]].replace(digit,"")
break
#Search for naked twins in diagonal units
for i in range(2): #scan through all the diagonal units
for index, box in enumerate(diagonal_units[i]):
if len(values[box]) == 2:
if index == 8:
#Prevent out of range error if box is last in the unit
continue
else:
for j in range(index+1,9):
if values[box] == values[diagonal_units[i][j]]:
if box in my_list_sq:
break
else:
#Naked twins were found.
#Add boxes to the list of already tested naked twins
my_list_diagonal.append(box)
my_list_diagonal.append(diagonal_units[i][j])
for k in range(9):
if (k != index and k != j) and len(values[diagonal_units[i][k]]) > 1:
flag = 1 #Set search continue flag
for digit in values[box]: #Remove digits in the naked twins from other box in the unit
values[diagonal_units[i][k]] = values[diagonal_units[i][k]].replace(digit,"")
break
else:
return values
def grid_values(grid):
"""
Convert grid into a dict of {square: char} with '123456789' for empties.
Args:
grid(string) - A grid in string form.
Returns:
A grid in dictionary form
Keys: The boxes, e.g., 'A1'
Values: The value in each box, e.g., '8'. If the box has no value, then the value will be '123456789'.
"""
grid_dict = {}
index = 0
#Scan through the grid row by row and replace all '.' by '123456789'
for i in [0, 9, 18, 27, 36, 45, 54, 63, 72]:
for j in range(9):
if grid[i+j] == '.':
grid_dict[row_units[index][j]] = '123456789'
else:
grid_dict[row_units[index][j]] = grid[i+j]
else:
index += 1
else:
return grid_dict
pass
def display(values):
"""
Display the values as a 2-D grid.
Args:
values(dict): The sudoku in dictionary form
"""
width = 1+max(len(values[s]) for s in boxes)
line = '+'.join(['-'*(width*3)]*3)
for r in rows:
print(''.join(values[r+c].center(width)+('|' if c in '36' else '')
for c in cols))
if r in 'CF': print(line)
return
pass
def eliminate(values):
my_list = []
#Scan through every box and store all the solved boxes in my_list
for box in boxes:
if len(values[box]) == 1:
my_list.append(box)
#Scan through my_list and eliminates its values from all its peers
for s_box in my_list:
num2elim = values[s_box] #Value to be eliminated
for p_vals in peers[s_box]:
if num2elim in values[p_vals]:
#Eliminate num2elim from peer box if it's present in possible solution
values[p_vals] = values[p_vals].replace(num2elim,'')
else:
return values
pass
def only_choice(values):
my_list = []
#Scan through every units(row, column, square, diagonal)
for unit in unitlist:
#print (unit)
for digit in '123456789': #Check every digit 1-9 for only choice possiblility in each unit
for box in unit: #Store all box in which the digit is present
if digit in values[box]:
my_list.append(box)
else:
#print("Length of list"+len(my_list))
if len(my_list) == 1: #If number of boxes of a digit is 1, then that's the only choice
values[my_list[0]] = digit
my_list[:] = []
else:
return values
pass
def reduce_puzzle(values):
solved_values = [box for box in values.keys() if len(values[box]) == 1]
stalled = False
while not stalled:
solved_values_before = len([box for box in values.keys() if len(values[box]) == 1])
values = eliminate(values)
values = only_choice(values)
solved_values_after = len([box for box in values.keys() if len(values[box]) == 1])
stalled = solved_values_before == solved_values_after
if len([box for box in values.keys() if len(values[box]) == 0]):
return False
return values
pass
def search(values):
#Check if there is an error
res = reduce_puzzle(values)
if res == False:
return False
#Check whether the sudoku has all single digit in it's boxes. If yes then
#considered solved
res = values
if all(len(values[s])==1 for s in boxes):
return values
#Find the box with the least search possibilities
search_box = find_search_box(values)
#Try each digit in the search box as a solution and solve sudoku. If it fails
#try the next digit and repeat till solution is found
for digit in values[search_box]:
new_sudoku = values.copy()
new_sudoku[search_box] = digit
attempt = search(new_sudoku)
if attempt:
return attempt
pass
def find_search_box(values):
"""
A function to find the box to be used to start the search method to solve
the sudoku.
"""
flag = 0
search_box = ''
for box in boxes: #Scan through all boxes
if len(values[box]) > 1: #Search should be unsolved
if len(values[box]) == 2: #If box has only two possible solutions, pick that box and return result
search_box = box
return search_box
break
if flag == 0: #If it is the first box, the store in variable
search_box = box
flag = 1
elif len(values[search_box]) > len(values[box]): #Check whether length of value in variable is greater, Store the shorter one
search_box = box
else:
return search_box
def solve(grid):
"""
Find the solution to a Sudoku grid.
Args:
grid(string): a string representing a sudoku grid.
Example: '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
Returns:
The dictionary representation of the final sudoku grid. False if no solution exists.
"""
values = grid_values(grid)
values = reduce_puzzle(values)
if values != False:
values = search(values)
if values != False:
return values
else:
print ('No solution. Returned False after search')
else:
print ('No solution. Returned False in reduced puzzle.')
if __name__ == '__main__':
diag_sudoku_grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
display(solve(diag_sudoku_grid))
try:
from visualize import visualize_assignments
visualize_assignments(assignments)
except SystemExit:
pass
except:
print('We could not visualize your board due to a pygame issue. Not a problem! It is not a requirement.')