-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku_solver.py
More file actions
executable file
·128 lines (114 loc) · 3.27 KB
/
sudoku_solver.py
File metadata and controls
executable file
·128 lines (114 loc) · 3.27 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
#!/usr/bin/env python3
from time import time
try:
import numpy as np
except ImportError:
print('Install numpy')
quit()
try:
from rich import print
except ImportError:
pass
'''
puzzle = [
[0, 1, 8, 9, 0, 0, 0, 0, 0],
[5, 2, 0, 0, 4, 0, 0, 0, 3],
[0, 0, 0, 0, 0, 3, 5, 1, 0],
[0, 0, 0, 0, 9, 0, 0, 5, 0],
[8, 0, 0, 0, 0, 0, 0, 0, 2],
[0, 7, 0, 0, 1, 0, 0, 0, 0],
[0, 9, 2, 6, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 5, 0, 0, 8, 6],
[0, 0, 0, 0, 0, 4, 1, 2, 0],]
'''
puzzle = [ [ y*0 for y in range(9) ] for x in range(9) ]
sudoku = np.matrix(puzzle)
start_time = 0
end_time = 0
def print_sudoku():
puzzle = sudoku.tolist()
for x in range(9):
if x % 3 :
print('\n\t', end = '')
else:
print(f'\n\t{"-" * 25}\n\t', end = '')
for y in range(9):
if y % 3 :
s = ""
else:
s = "| "
print(f"{s}{puzzle[x][y]} ", end = '')
if y == 8: print("|", end = '')
print(f'\n\t{"-" * 25}\n\t', end = '')
print('\n')
def check(row, column, value):
if value in sudoku[row,:]:
return False
for i in range(9):
if value == sudoku[i,column]:
return False
blk_row = (row//3)*3
blk_column = (column//3)*3
for x in range(blk_row, blk_row + 3):
for y in range(blk_column, blk_column + 3):
if sudoku[x,y] == value:
return False
return True
def solve():
global sudoku
for row in range(9):
for column in range(9):
if not sudoku[row, column]:
for value in range(1,10):
if check(row, column, value):
sudoku[row, column] = value
solve()
sudoku[row, column] = 0
return
global end_time, start_time
end_time = time()
print_sudoku()
if 'y' not in input("\nTry to find another solution ? : ").lower():
quit()
start_time += time() - end_time
def validate():
global puzzle, sudoku
for i in range(9):
while len(puzzle[i]) < 9:
puzzle[i].append(0)
while len(puzzle[i]) > 9:
puzzle[i].pop(-1)
sudoku = np.matrix(puzzle)
print_sudoku()
def main():
global sudoku, start_time, puzzle
print_sudoku()
if 'n' not in input("Get puzzle from file ? : ").lower():
file_path = input("Enter file name : ")
if not file_path:
file_path = 'sudoku1.txt'
with open(file_path) as file:
for i, line in enumerate(file.readlines()):
puzzle[i] = [ int(x) for x in line.split() ]
validate()
while 'n' in input("Use defined sudoku ? : ").lower():
for i in range(9):
puzzle[i] = [int(x) for x in input(f"Enter row {i + 1} : ")]
validate()
start_time = time()
try:
solve()
except KeyboardInterrupt:
print_sudoku()
quit()
finally:
if end_time:
tat = abs(end_time - start_time)
print(f"Time Required = {round(tat,4)} seconds")
else:
print("Invalid puzzle")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
quit()