-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
165 lines (139 loc) · 5.39 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
import sys
import numpy as np
from datetime import datetime
from copy import deepcopy
def solve_sudoku(sudoku_array):
# https://stackoverflow.com/questions/1697334/algorithm-for-solving-sudoku von:dominik von stack overflow
# HIER ANJAS TEIL
# Eingabe: Sudoku als mehrdimensionales Array (je Reihe ein Unter-Array),
# z.B: [[5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], ...]
# die Nullen entsprechen den leeren Sudoku-Feldern
# Source: https://stackoverflow.com/a/35500280/3582159
def output(a):
sys.stdout.write(str(a))
N = len(sudoku_array)
def print_field(field):
if not field:
output("No solution")
return
for i in range(N):
for j in range(N):
cell = field[i][j]
if cell == 0 or isinstance(cell, set):
output('.')
else:
output(cell)
if (j + 1) % 3 == 0 and j < 8:
output(' |')
if j != 8:
output(' ')
output('\n')
if (i + 1) % 3 == 0 and i < 8:
output("- - - + - - - + - - -\n")
def read(field):
""" Read field into state (replace 0 with set of possible values) """
state = deepcopy(field)
for i in range(N):
for j in range(N):
cell = state[i][j]
if cell == 0:
state[i][j] = set(range(1, 10))
return state
def done(state):
""" Are we done? """
for row in state:
for cell in row:
if isinstance(cell, set):
return False
return True
def propagate_step(state):
""" Propagate one step """
new_units = False
for i in range(N):
row = state[i]
values = set([x for x in row if not isinstance(x, set)])
for j in range(N):
if isinstance(state[i][j], set):
state[i][j] -= values
if len(state[i][j]) == 1:
state[i][j] = state[i][j].pop()
new_units = True
elif len(state[i][j]) == 0:
return False, None
for j in range(N):
column = [state[x][j] for x in range(N)]
values = set([x for x in column if not isinstance(x, set)])
for i in range(N):
if isinstance(state[i][j], set):
state[i][j] -= values
if len(state[i][j]) == 1:
state[i][j] = state[i][j].pop()
new_units = True
elif len(state[i][j]) == 0:
return False, None
for x in range(3):
for y in range(3):
values = set()
for i in range(3 * x, 3 * x + 3):
for j in range(3 * y, 3 * y + 3):
cell = state[i][j]
if not isinstance(cell, set):
values.add(cell)
for i in range(3 * x, 3 * x + 3):
for j in range(3 * y, 3 * y + 3):
if isinstance(state[i][j], set):
state[i][j] -= values
if len(state[i][j]) == 1:
state[i][j] = state[i][j].pop()
new_units = True
elif len(state[i][j]) == 0:
return False, None
return True, new_units
def propagate(state):
""" Propagate until we reach a fixpoint """
while True:
solvable, new_unit = propagate_step(state)
if not solvable:
return False
if not new_unit:
return True
def solve(state):
""" Solve sudoku """
solvable = propagate(state)
if not solvable:
return None
if done(state):
return state
for i in range(N):
for j in range(N):
cell = state[i][j]
# Do not solve if solving takes more than 1/30secs.
curtime = datetime.now()
#print((curtime - starttime).microseconds)
if (curtime - starttime).microseconds > (1000000/3):
print("jumped solving because took too long")
return None
if isinstance(cell, set):
for value in cell:
new_state = deepcopy(state)
new_state[i][j] = value
solved = solve(new_state)
if solved is not None:
return solved
return None
starttime = datetime.now()
print("started solving")
res = None
# do not solve sudokus with less than 5 known numbers (of 81)
if(np.count_nonzero(sudoku_array) >= 5):
state = read(sudoku_array)
res = solve(state)
if res is not None:
print("solved")
else:
print("could not solve")
#print_field(res)
else:
print("jumped solving because less than 5 known numbers")
# Ausgabe: gelöstes Sudoku in der gleichen Struktur oder wenn unlösbar: original-sudoku
return res if res else sudoku_array