-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.py
More file actions
executable file
·149 lines (130 loc) · 3.41 KB
/
Copy pathsudoku.py
File metadata and controls
executable file
·149 lines (130 loc) · 3.41 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
#! /usr/bin/env python3
import sys
import time
import types
level1 = ["__9__7_5_",
"_628_59_4",
"4_39_2__7",
"_15______",
"____2____",
"______64_",
"3__1_65_9",
"9_65_387_",
"_7_2__4__"]
level3 = ["5___7____",
"_49_3_762",
"_1_4___8_",
"__15_____",
"____6____",
"_____84__",
"_9___3_7_",
"728_5_64_",
"____4___1"]
level4 = ["6___9____",
"427__8_6_",
"__9__27__",
"____25___",
"_6_7_1_8_",
"___38____",
"__62__5__",
"_4_5__328",
"____7___6"]
level5 = ["7___8_5_1",
"___5___4_",
"_4____9__",
"_56__73__",
"_1_2_5_8_",
"__91__76_",
"__4____5_",
"_6___1___",
"9_2_5___4"]
stats = types.SimpleNamespace()
stats.steps = 0
stats.branches = 0
grid = 81 * [0]
def print_grid():
for i in range(0, 9):
row = ''
for j in range(0, 9):
n = grid[coord(i, j)]
row += ' ' + str(n)
print(row)
def coord(i, j):
return 9 * i + j
def assign(i, j, n):
grid[coord(i, j)] = n
def init(data):
for i, row in enumerate(data):
for j, col in enumerate(row):
if col != '_':
assign(i, j, int(col))
def possible():
poss = 81 * [None]
for i in range(0, 9):
for j in range(0, 9):
n = grid[coord(i, j)]
if n:
s = set()
else:
s = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
for p in range(0, 9):
s.discard(grid[coord(p, j)])
for q in range(0, 9):
s.discard(grid[coord(i, q)])
k = i - i % 3
l = j - j % 3
for m in range(k, k + 3):
for n in range(l, l + 3):
s.discard(grid[coord(m, n)])
poss[coord(i, j)] = s
return poss
class DoneError(Exception):
pass
def select(active_branches):
poss = possible()
alt = 10 * [None]
for i in range(0, 10):
alt[i] = []
for i in range(0, 9):
for j in range(0, 9):
s = poss[coord(i, j)]
alt[len(s)].append((i, j, s))
if len(alt[0]) == 81:
not_done = False
for i in range(0, 81):
if not grid[i]:
not_done = True
if not not_done:
print(active_branches)
raise DoneError()
return None
for i in range(1, 10):
if alt[i]:
return alt[i][0]
def solve(active_branches):
selection = select(active_branches)
if selection:
i, j, s = selection
if len(s) > 1:
active_branches = active_branches[:] + [[i, j, None, list(s)]]
print(len(active_branches) * ' ', i, j, list(s))
stats.branches += 1
for n in s:
stats.steps += 1
grid[coord(i, j)] = n
if len(s) > 1:
active_branches[-1][2] = n
solve(active_branches)
grid[coord(i, j)] = 0
def main():
start = time.monotonic_ns()
init(level5)
try:
solve([])
except DoneError:
print(stats)
print_grid()
stop = time.monotonic_ns()
print('time elapsed', (stop - start) / 1000000.0, 'ms')
if __name__ == '__main__':
main()