-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
307 lines (251 loc) · 7.63 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
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
import numpy as np
src = [
' 6 13 89',
' 4 ',
' 4 9 35',
'5 94 ',
'6 1',
' 72 4',
'92 3 8 ',
' 6 ',
'48 62 5 '
]
symbols = '123456789'
numbers = [
]
masks = {}
def initial_populate_numbers(src, numbers):
for line in src:
nr_line = []
for c in line:
nr = c
if c == ' ':
nr = None
nr_line.append(nr)
numbers.append(nr_line)
def initial_populate_masks(masks):
for s in symbols:
mask = []
for i in range(9):
line = []
for j in range(9):
line.append(False)
mask.append(line)
masks[s] = mask
def get(array, x, y):
return array[y][x]
def set(array, x, y, val):
array[y][x] = val
def print_horiz():
print('|---------+---------+---------|')
def print_array(picker):
for line_count in range(9):
if line_count % 3 == 0:
print_horiz()
for col_count in range(9):
if col_count % 3 == 0:
print('|', end='')
c = picker(col_count, line_count)
print(' ' + c + ' ', end='')
print('|')
print_horiz()
def print_numbers(numbers):
def picker(col, line):
nr = get(numbers, col, line)
if nr:
return nr
else:
return ' '
print_array(picker)
def print_mask(numbers, masks, symbol):
def picker(x, y):
val = get(masks[symbol], x, y)
if val:
return 'X'
else:
if get(numbers, x, y) == symbol:
return '+'
return ' '
print("Mask " + symbol + ':')
print_array(picker)
def paint_horiz(mask, x, y):
for i in range(9):
if int(x / 3) != int(i / 3):
set(mask, i, y, True)
def paint_vert(mask, x, y):
for i in range(9):
if int(y / 3) != int(i / 3):
set(mask, x, i, True)
def paint_box(mask, x, y):
bx = 3 * int(x / 3)
by = 3 * int(y / 3)
for i in range(bx, bx + 3):
for j in range(by, by + 3):
if x != i or y != j:
set(mask, i, j, True)
# Set the mask for all locations where there is a number (except this symbol)
def mask_other_numbers(numbers, mask, symbol):
for x in range(9):
for y in range(9):
s = get(numbers, x, y)
if s and s != symbol:
set(mask, x, y, True)
# Set the mask for all locations (except the symbol location it self) in all boxes where this symbol exists
def mask_self(numbers, mask, symbol):
for x in range(9):
for y in range(9):
s = get(numbers, x, y)
if s == symbol:
paint_box(mask, x, y)
# Find columns where the symbol forms a single line in a box and mask out the rest of those columns
def mask_single_cols(mask):
for i in range(3):
yb = i * 3
for j in range(3):
xb = j * 3
all_masked = [True, True, True]
for x in range(xb, xb + 3):
for y in range(yb, yb + 3):
if not get(mask, x, y):
all_masked[x - xb] = False
count = 0
last_found = -1
for i in range(3):
masked = all_masked[i]
if not masked:
count += 1
last_found = i
if count == 1:
paint_vert(mask, last_found + xb, yb)
# Find rows where the symbol forms a single line in a box and mask out the rest of those rows
def mask_single_row(mask):
for i in range(3):
yb = i * 3
for j in range(3):
xb = j * 3
all_masked = [True, True, True]
for x in range(xb, xb + 3):
for y in range(yb, yb + 3):
if not get(mask, x, y):
all_masked[y - yb] = False
count = 0
last_found = -1
for i in range(3):
masked = all_masked[i]
if not masked:
count += 1
last_found = i
if count == 1:
paint_horiz(mask, xb, last_found + yb)
# Search a column to see if there is one one hole in the mask
def find_single_hole_in_col(mask, x):
count = 0
last_found = -1
for y in range(9):
if not get(mask, x, y):
count += 1
last_found = y
if count == 1:
return last_found
else:
return None
# Search a row to see if there is one one hole in the mask
def find_single_hole_in_row(mask, y):
count = 0
last_found = -1
for x in range(9):
if not get(mask, x, y):
count += 1
last_found = x
if count == 1:
return last_found
else:
return None
# Search a box to see if there is one one hole in the mask
def find_single_hole_in_box(mask, x, y):
count = 0
last_found_x = -1
last_found_y = -1
xb = 3 * int(x / 3)
yb = 3 * int(y / 3)
for x in range(xb, xb + 3):
for y in range(yb, yb + 3):
if not get(mask, x, y):
count += 1
last_found_x = x
last_found_y = y
if count == 1:
return last_found_x, last_found_y
else:
return None, None
# Search all masks for a location to see if there is only one possible symbol
def find_only_sym_for_position(masks, x, y):
found = None
count = 0
for symbol in symbols:
if not get(masks[symbol], x, y):
found = symbol
count += 1
if count == 1:
return found
return None
def iterate_mask(numbers, masks, symbol):
mask = masks[symbol]
mask_other_numbers(numbers, mask, symbol)
mask_self(numbers, mask, symbol)
mask_single_cols(mask)
mask_single_row(mask)
def iterate_number(numbers, masks, symbol):
mask = masks[symbol]
# Columns
for x in range(9):
y = find_single_hole_in_col(mask, x)
if y and get(numbers, x, y) != symbol:
print('found', symbol, 'in col @ ', x, y)
set(numbers, x, y, symbol)
# Rows
for y in range(9):
x = find_single_hole_in_row(mask, y)
if x and get(numbers, x, y) != symbol:
print('found', symbol, 'in row @ ', x, y)
set(numbers, x, y, symbol)
# Boxes
for i in range(3):
for j in range(3):
x, y = find_single_hole_in_box(mask, i * 3, j * 3)
if x and y and get(numbers, x, y) != symbol:
print('found', symbol, 'in box @ ', x, y)
set(numbers, x, y, symbol)
def iterate_single_fit(numbers, masks):
for x in range(9):
for y in range(9):
symbol = find_only_sym_for_position(masks, x, y)
if symbol and get(numbers, x, y) != symbol:
print('found', symbol, 'as single fit @ ', x, y)
set(numbers, x, y, symbol)
def count_reminder(numbers):
count = 0
for line in numbers:
for sym in line:
if sym == None:
count += 1
return count
initial_populate_numbers(src, numbers)
initial_populate_masks(masks)
print_numbers(numbers)
last_remains = 100000;
for i in range(30):
for symbol in symbols:
iterate_mask(numbers, masks, symbol)
# print_mask(numbers, masks, symbol)
iterate_number(numbers, masks, symbol)
iterate_single_fit(numbers, masks)
remains = count_reminder(numbers)
print_numbers(numbers)
print(remains, 'unsolved')
if remains == 0 or remains == last_remains:
break
last_remains = remains
print()
# for symbol in symbols:
# print_mask(numbers, masks, symbol)