-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
268 lines (172 loc) · 4.54 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
import copy
puzzle = [
[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]
]
# one solution
puzzle = [
[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]
]
#multiple solutions
puzzle = [
[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]
]
original_puzzle = copy.deepcopy(puzzle)
def convert_to_lists(puzzle):
for row in range(0, 9):
for col in range(0, 9):
value = puzzle[row][col]
if value == 0:
puzzle[row][col] = []
else:
puzzle[row][col] = [value]
def validateSet(set):
validSet = True
check = {}
for num in range(1,10):
check[num] = 0
for numbers in set:
if len(numbers) == 1:
number = numbers[0]
else:
number = 0
if number > 0:
if check[number] > 0:
validSet = False
check[number] = check[number] + 1
return validSet
def getRow(puzzle, number):
return puzzle[number - 1]
def getColumn(puzzle, number):
col = []
for row in puzzle:
col.append(row[number - 1])
return col
def getBlock(puzzle, blockRow, blockCol):
block = []
for row in range(0,3):
for col in range(0,3):
block.append(puzzle[row + (3 * (blockRow - 1))][col + (3 * (blockCol - 1))])
return block;
def validatePuzzle(puzzle):
valid = True
for num in range(1, 10):
valid = valid and validateSet(getColumn(puzzle, num))
for num in range(1, 10):
valid = valid and validateSet(getRow(puzzle, num))
for row in range(0, 3):
for col in range(0, 3):
valid = valid and validateSet(getBlock(puzzle, row, col))
return valid
def add_possible_values(puzzle):
for row in range(0, 9):
for col in range(0, 9):
value = puzzle[row][col]
if len(value) != 1:
puzzle[row][col] = 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(puzzle[row - 1][col - 1]) > 0:
possible_values = puzzle[row - 1][col - 1].copy()
#print(getRow(puzzle, row))
for nums in getRow(puzzle, row):
if len(nums) == 1:
try:
possible_values.remove(nums[0])
except ValueError:
pass
#print(getColumn(puzzle, col))
for nums in getColumn(puzzle, col):
if len(nums) == 1:
try:
possible_values.remove(nums[0])
except ValueError:
pass
blockCoordinates = get_block_coordinates(row, col)
for nums in getBlock(puzzle, blockCoordinates[0], blockCoordinates[1]):
if len(nums) == 1:
try:
possible_values.remove(nums[0])
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):
print(puzzle[row])
def test_solution(puzzle):
finished = True
for row in range(0, 9):
for col in range(0, 9):
if len(puzzle[row][col]) > 1:
finished = False
if len(puzzle[row][col]) == 0:
finished = False
#print(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 = puzzle[row][col]
#print(possible_values)
if len(possible_values) != 1:
for num in possible_values:
new_puzzle = copy.deepcopy(puzzle)
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)
#for nums in getRow(puzzle, 1):
# print(nums)
convert_to_lists(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)