-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver_faster.py
340 lines (221 loc) · 5.91 KB
/
solver_faster.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
import copy
#multiple solutions
puzzle3 = [
[1,2,3,0,0,6,7,8,9],
[4,5,6,7,8,0,1,2,3],
[7,8,0,0,2,3,4,5,6],
[0,9,1,0,4,5,6,7,8],
[0,0,0,3,9,1,2,0,5],
[0,4,0,0,0,0,0,9,1],
[0,0,0,8,3,4,0,0,7],
[9,3,0,0,6,0,8,1,4],
[8,6,0,0,0,0,5,3,2]
]
# one solution
puzzle2 = [
[1,2,3,0,0,6,7,8,9],
[4,5,6,7,8,0,1,2,3],
[7,8,9,1,2,3,4,5,6],
[3,9,1,0,4,5,6,7,8],
[6,7,8,3,9,1,2,0,5],
[2,4,0,0,0,0,0,9,1],
[5,0,0,8,3,4,0,0,7],
[9,3,0,0,6,0,8,1,4],
[8,6,0,0,0,0,5,3,2]
]
# simple puzzle
puzzle1 = [
[1,2,3,4,5,6,7,8,9],
[4,5,6,7,8,9,1,2,3],
[7,8,9,1,2,3,4,5,6],
[3,9,1,2,4,5,6,7,8],
[6,7,8,3,9,1,2,0,5],
[2,4,5,6,7,8,3,9,1],
[5,1,2,8,3,4,9,6,7],
[9,3,7,5,6,2,8,1,4],
[8,6,0,0,0,0,5,3,2]
]
puzzle4 = [
[1,0,0,0,0,0,0,0,0],
[0,2,0,0,0,0,0,0,0],
[0,0,3,0,0,0,0,0,0],
[0,0,0,4,0,0,0,0,0],
[0,0,0,0,5,0,0,0,0],
[0,0,0,0,0,6,0,0,0],
[0,0,0,0,0,0,7,0,0],
[0,0,0,0,0,0,0,8,0],
[0,0,0,0,0,0,0,0,9]
]
#original_puzzle = copy.deepcopy(puzzle)
def convert_to_list(puzzle):
puzzle_list = []
for row in range(0, 9):
for col in range(0, 9):
value = puzzle[row][col]
if value == 0:
puzzle_list.append('')
else:
puzzle_list.append(str(value))
return puzzle_list
def validateSet(set):
validSet = True
check = {}
for num in range(1,10):
check[str(num)] = 0
for numbers in set:
number = numbers
if len(number) > 0:
if check[number] > 0:
validSet = False
check[number] = check[number] + 1
return validSet
def getRow(puzzle, number):
row = []
for num in range(0,9):
index = num + ((number - 1) * 9)
row.append(puzzle[index])
return row
def getColumn(puzzle, number):
col = []
for num in range(0,9):
col.append(puzzle[(num * 9) + (number - 1)])
return col
def getBlock(puzzle, blockRow, blockCol):
block = []
for row in range(0,3):
for col in range(0,3):
index = ((row + (3 * (blockRow - 1))) * 9) + (col + (3 * (blockCol - 1)))
block.append(puzzle[index])
return block
def getIndex(row, col):
return ((row * 9) + col)
def getValue(puzzle, row, col):
return puzzle[getIndex(row, col)]
def validatePuzzle(puzzle):
valid = True
for num in range(1, 10):
#print('column')
valid = valid and validateSet(getColumn(puzzle, num))
for num in range(1, 10):
#print('row')
valid = valid and validateSet(getRow(puzzle, num))
for row in range(1, 4):
for col in range(1, 4):
#print('block')
valid = valid and validateSet(getBlock(puzzle, row, col))
return valid
def setValue(puzzle, row, col, value):
puzzle[getIndex(row, col)] = value
def add_possible_values(puzzle):
for row in range(0, 9):
for col in range(0, 9):
value = getValue(puzzle, row, col)
if len(str(value)) != 1:
setValue(puzzle, row, col, ','.join(get_possible_values(puzzle, row + 1, col + 1)))
def get_possible_values(puzzle, row, col):
possible_values = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
if len(str(getValue(puzzle, row - 1, col - 1))) > 0:
possible_values = getValue(puzzle, row - 1, col - 1).split(',')
#print(getRow(puzzle, row))
for nums in getRow(puzzle, row):
if len(str(nums)) == 1:
try:
possible_values.remove(str(nums))
except ValueError:
pass
#print(getColumn(puzzle, col))
for nums in getColumn(puzzle, col):
if len(str(nums)) == 1:
try:
possible_values.remove(str(nums))
except ValueError:
pass
blockCoordinates = get_block_coordinates(row, col)
for nums in getBlock(puzzle, blockCoordinates[0], blockCoordinates[1]):
if len(str(nums)) == 1:
try:
possible_values.remove(str(nums))
except ValueError:
pass
return possible_values;
def get_block_coordinates(row, col):
blockRow = convert_coordinate(row)
blockCol = convert_coordinate(col)
return [blockRow, blockCol]
def convert_coordinate(coor):
blockCoor = 0
if(coor == 1 or coor == 2 or coor == 3):
blockCoor = 1
if(coor == 4 or coor == 5 or coor == 6):
blockCoor = 2
if(coor == 7 or coor == 8 or coor == 9):
blockCoor = 3
return blockCoor
def printPuzzle(puzzle):
for row in range(0, 9):
rowList = []
for col in range(0, 9):
rowList.append(getValue(puzzle, row, col))
print(rowList)
def test_solution(puzzle):
finished = True
for row in range(0, 9):
for col in range(0, 9):
value = getValue(puzzle, row, col)
if len(value) > 1:
finished = False
if len(value) == 0:
finished = False
#print(finished)
if finished:
finished = finished and validatePuzzle(puzzle)
#print(finished)
return finished
def find_solutions(puzzle):
#print('find_solution called')
#printPuzzle(puzzle)
solution_found = True
for row in range(0, 9):
for col in range(0, 9):
possible_values = getValue(puzzle, row, col).split(',')
#print(possible_values)
if len(possible_values) != 1:
for num in possible_values:
new_puzzle = puzzle.copy()
setValue(new_puzzle, row, col, num)
#printPuzzle(new_puzzle)
if(test_solution(new_puzzle)):
print('solution found')
printPuzzle(new_puzzle)
add_possible_values(new_puzzle)
find_solutions(new_puzzle)
def testGetFunctions(puzzle):
for blockRow in range(1,4):
for blockCol in range(1,4):
print('block')
print([blockRow, blockCol])
print(getBlock(puzzle, blockRow, blockCol))
for row in range(1, 10):
print('row')
print(row)
print(getRow(puzzle, row))
for col in range(1, 10):
print('column')
print(col)
print(getColumn(puzzle, col))
for row in range(0, 9):
for col in range(0, 9):
print([row, col])
print(getValue(puzzle, row, col))
#for nums in getRow(puzzle, 1):
# print(nums)
puzzle = convert_to_list(puzzle3)
#testGetFunctions(puzzle)
#print(validatePuzzle(puzzle))
#print(getBlock(puzzle, 2, 2))
#print(get_possible_values(puzzle, 9, 9))
if(validatePuzzle(puzzle)):
add_possible_values(puzzle)
printPuzzle(puzzle)
find_solutions(puzzle)
#printPuzzle(original_puzzle)