-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuman_solver_print.py
More file actions
480 lines (382 loc) · 15.2 KB
/
Copy pathhuman_solver_print.py
File metadata and controls
480 lines (382 loc) · 15.2 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
from boards import *
from print_board import *
import time
# Look for next empty position that has not already been looked at
def next_empty(puzzle, no_solution):
for row in range(0, 9):
for col in range(0, 9):
if puzzle[row][col] == "*":
if (row, col) not in no_solution:
return row, col
return None, None
def single_position_row(puzzle, row, col, guess):
for i in range (0, 9):
if puzzle[row][i] == guess: # guess already exists in row
return False
return True
def single_position_col(puzzle, row, col, guess):
for i in range (0, 9):
if puzzle[i][col] == guess: # guess already exists in column
return False
return True
def single_position_group(puzzle, row, col, guess):
row_start = (row // 3) * 3
col_start = (col // 3) * 3
for r in range(row_start, row_start + 3):
for c in range(col_start, col_start + 3):
if puzzle[r][c] == guess: # guess already exists in subgroup
return False
return True
def determine_occupied(row, col):
vertical_occupied = []
horizontal_occupied = []
col_adj1, col_adj2, row_adj1, row_adj2 = find_adjacent(row, col)
if puzzle[row_adj1][col] != '*':
vertical_occupied.append(True)
else:
vertical_occupied.append(False)
if puzzle[row_adj2][col] != '*':
vertical_occupied.append(True)
else:
vertical_occupied.append(False)
if puzzle[row][col_adj1] != '*':
horizontal_occupied.append(True)
else:
horizontal_occupied.append(False)
if puzzle[row][col_adj2] != '*':
horizontal_occupied.append(True)
else:
horizontal_occupied.append(False)
return vertical_occupied, horizontal_occupied
def find_adjacent(row, col):
if col in [0, 3, 6]: # curr # adj1 # adj2
col_adj1 = col+1
col_adj2 = col+2
elif col in [1, 4, 7]: # adj1 # curr # adj2
col_adj1 = col-1
col_adj2 = col+1
else: # adj1 # adj2 # curr
col_adj1 = col-2
col_adj2 = col-1
if row in [0, 3, 6]: # curr
row_adj1 = row+1 # adj1
row_adj2 = row+2 # adj2
elif row in [1, 4, 7]: # adj1
row_adj1 = row-1 # curr
row_adj2 = row+1 # adj2
else: # adj1
row_adj1 = row-2 # adj2
row_adj2 = row-1 # curr
return col_adj1, col_adj2, row_adj1, row_adj2
def adjacent_col(puzzle, row, col, guess):
result = []
check_1 = False
check_2 = False
col_adj1, col_adj2, row_adj1, row_adj2 = find_adjacent(row, col)
for i in range (0, 9):
if puzzle[i][col_adj1] == guess: # guess already exists in adjacent column
check_1 = True
if puzzle[i][col_adj2] == guess: # guess already exists in adjacent column
check_2 = True
result.append(check_1)
result.append(check_2)
return result
def adjacent_row(puzzle, row, col, guess):
result = []
check_1 = False
check_2 = False
col_adj1, col_adj2, row_adj1, row_adj2 = find_adjacent(row, col)
for i in range (0, 9):
if puzzle[row_adj1][i] == guess: # guess already exists in adjacent row
check_1 = True
if puzzle[row_adj2][i] == guess: # guess already exists in adjacent row
check_2 = True
result.append(check_1)
result.append(check_2)
return result
def check_other_candidates(guess, row_pos, col_pos, candidates_dict):
horizontal = False
vertical = False
for key in candidates_dict.keys():
if key[0] == row_pos:
if key != (row_pos, col_pos):
if guess in candidates_dict[key]:
horizontal = True
if key[1] == col_pos:
if key != (row_pos, col_pos):
if guess in candidates_dict[key]:
vertical = True
result = horizontal and vertical
return result
def select_possible_candidate(puzzle, row_pos, col_pos, candidates, candidates_dict):
vertical_occupied, horizontal_occupied = determine_occupied(row_pos, col_pos)
vertical = vertical_occupied[0] and vertical_occupied[1]
horizontal = horizontal_occupied[0] and horizontal_occupied[1]
for guess in candidates:
adj_col_result = adjacent_col(puzzle, row_pos, col_pos, guess)
adj_col = adj_col_result[0] and adj_col_result[1]
adj_row_result = adjacent_row(puzzle, row_pos, col_pos, guess)
adj_row = adj_row_result[0] and adj_row_result[1]
if adj_col and adj_row:
puzzle[row_pos][col_pos] = guess
del candidates_dict[(row_pos, col_pos)]
return True
elif vertical and adj_col:
puzzle[row_pos][col_pos] = guess # |8
del candidates_dict[(row_pos, col_pos)] # |6
return True # |X
# | 1 |
# ...
# 1|
elif horizontal and adj_row:
puzzle[row_pos][col_pos] = guess
del candidates_dict[(row_pos, col_pos)]
return True
elif adj_row and horizontal_occupied[1] and adj_col_result[0]:
puzzle[row_pos][col_pos] = guess # |6 |
del candidates_dict[(row_pos, col_pos)] # 6 | |
return True # | | 6
elif adj_row and horizontal_occupied[0] and adj_col_result[1]: # |* X 1|
puzzle[row_pos][col_pos] = guess
del candidates_dict[(row_pos, col_pos)]
return True
elif adj_col and vertical_occupied[0] and adj_row_result[1]:
puzzle[row_pos][col_pos] = guess
del candidates_dict[(row_pos, col_pos)]
return True
elif adj_col and vertical_occupied[1] and adj_row_result[0]:
puzzle[row_pos][col_pos] = guess
del candidates_dict[(row_pos, col_pos)]
return True
# if guess is not candidate for any other cell in row or column
elif not check_other_candidates(guess, row_pos, col_pos, candidates_dict):
puzzle[row_pos][col_pos] = guess
del candidates_dict[(row_pos, col_pos)]
return True
return False
def valid_guess(puzzle, row_pos, col_pos, guess):
row_result = single_position_row(puzzle, row_pos, col_pos, guess)
col_result = single_position_col(puzzle, row_pos, col_pos, guess)
group_result = single_position_group(puzzle, row_pos, col_pos, guess)
if row_result and col_result and group_result:
return True
else:
return False
def initialize_candidate_dict(puzzle):
candidates_dict = {}
for row in range(0, 9):
for col in range(0, 9):
if puzzle[row][col] == "*":
candidates_dict[(row, col)] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
return candidates_dict
def human_solve_sudoku(puzzle):
solved = False
candidates_dict = initialize_candidate_dict(puzzle)
no_solution = []
while solved == False:
candidates = []
row_pos, col_pos = next_empty(puzzle, no_solution)
if row_pos is None and col_pos is None:
if not no_solution: # stop if puzzle is solved
#print("\nPuzzle solved")
solved = True
break
else:
no_solution = []
row_pos, col_pos = next_empty(puzzle, no_solution)
for guess in range(1, 10):
if valid_guess(puzzle, row_pos, col_pos, guess):
candidates.append(guess)
# print(f"row_pos: {row_pos}, col_pos: {col_pos}")
candidates_dict[(row_pos, col_pos)] = candidates
if len(candidates) == 1:
puzzle[row_pos][col_pos] = candidates[0] # single candidate, write to board
del candidates_dict[(row_pos, col_pos)]
else:
# select between multiple candidates
# if no candidates selected then proceed
if not (select_possible_candidate(puzzle, row_pos, col_pos, candidates, candidates_dict)):
# no current solution for position
no_solution.append((row_pos, col_pos))
if __name__ == '__main__':
print(f"Rule Based Searching........")
start_time = time.time()
puzzle = board_0()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 0: {time.time() - start_time}")
current_time = time.time()
puzzle = board_1()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 1: {time.time() - current_time}")
current_time = time.time()
puzzle = board_2()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 2: {time.time() - current_time}")
current_time = time.time()
puzzle = board_3()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 3: {time.time() - current_time}")
current_time = time.time()
puzzle = board_4()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 4: {time.time() - current_time}")
print(f"TIME TAKEN TO SOLVE 5 BOARDS: {time.time() - start_time}")
current_time = time.time()
puzzle = board_5()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 5: {time.time() - current_time}")
current_time = time.time()
puzzle = board_6()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 6: {time.time() - current_time}")
current_time = time.time()
puzzle = board_7()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 7: {time.time() - current_time}")
current_time = time.time()
puzzle = board_8()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 8: {time.time() - current_time}")
current_time = time.time()
puzzle = board_9()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 9: {time.time() - current_time}")
print(f"TIME TAKEN TO SOLVE 10 BOARDS: {time.time() - start_time}")
current_time = time.time()
puzzle = board_10()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 10: {time.time() - current_time}")
current_time = time.time()
puzzle = board_11()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 11: {time.time() - current_time}")
current_time = time.time()
puzzle = board_12()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 12: {time.time() - current_time}")
current_time = time.time()
puzzle = board_13()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 13: {time.time() - current_time}")
current_time = time.time()
puzzle = board_14()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 14: {time.time() - current_time}")
print(f"TIME TAKEN TO SOLVE 15 BOARDS: {time.time() - start_time}")
current_time = time.time()
puzzle = board_15()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 15: {time.time() - current_time}")
current_time = time.time()
puzzle = board_16()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 16: {time.time() - current_time}")
current_time = time.time()
puzzle = board_17()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 17: {time.time() - current_time}")
current_time = time.time()
puzzle = board_18()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 18: {time.time() - current_time}")
current_time = time.time()
puzzle = board_19()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 19: {time.time() - current_time}")
print(f"TIME TAKEN TO SOLVE 20 BOARDS: {time.time() - start_time}")
current_time = time.time()
puzzle = board_20()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 20: {time.time() - current_time}")
current_time = time.time()
puzzle = board_21()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 21: {time.time() - current_time}")
current_time = time.time()
puzzle = board_22()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 22: {time.time() - current_time}")
current_time = time.time()
puzzle = board_23()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 23: {time.time() - current_time}")
current_time = time.time()
puzzle = board_24()
print_board(puzzle)
print("\n")
human_solve_sudoku(puzzle)
print_board(puzzle)
print(f"Time taken to solve Board 24: {time.time() - current_time}")
print(f"TIME TAKEN TO SOLVE 25 BOARDS: {time.time() - start_time}")