-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudokuGenerator2.py
454 lines (373 loc) · 12.6 KB
/
sudokuGenerator2.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
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
# This is a sudoku generator, which generates a unique sudoku randomly
# But unrandomly the sudoku it generates won't have any extraneous digits
# Since it's so random the sudoku aren't necessarily good
# Often half of the grid is filled by singles
# In fact the following sudoku is super easy, except for this one WXYZ wing at the end.
# 240100000
# 007800600
# 010003900
# 000701000
# 000004200
# 000900870
# 060020000
# 005030004
# 070000050
# So far it seems to take anywhere from a to b seconds:
# 2.770194907s (wow!)
# 819.054982936s (WOWWWWWWW!)
# It takes so long since it checks a sudoku's validity by bruteforce
# So the longer it takes, the harder it is to bruteforce the solution.
# And generally hard to bruteforce sudokus are ... vaguely harder to solve.
# Here's the 819 second sudoku (hard)
"""
004000000
805000000
070500030
000000800
000706004
000400316
009100070
300080100
007090005
"""
# Here's the 2.770194907 second sudoku (somewhat easy)
"""
006040000
050000019
812050004
080239000
090084000
004500000
001000003
600020700
070000400
"""
# Credits to www.101computing.net/sudoku-generator-algorithm/
# Modified
# import turtle
import random
from random import shuffle
import time
from time import sleep
starttime = time.time_ns()
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
digitIndices = [digit - 1 for digit in digits]
# initialise empty 9 by 9 grid
grid = []
for row in range(9):
grid.append([])
for _ in range(9):
grid[row].append([1, 2, 3, 4, 5, 6, 7, 8, 9])
# myPen = turtle.Turtle()
# turtle.tracer(0)
# myPen.speed(0)
# myPen.color("#000000")
# myPen.hideturtle()
# # Drawing variables
# topLeft_x = -150
# topLeft_y = 150
# intDim = 35
# def text(message, x, y, size):
# FONT = ('Arial', size, 'normal')
# myPen.penup()
# myPen.goto(x, y)
# myPen.write(message, align="left", font=FONT)
# # A procedure to draw the grid on screen using Python Turtle
# def drawGrid(grid):
# for row in digitIndices:
# if (row % 3) == 0:
# myPen.pensize(3)
# else:
# myPen.pensize(1)
# myPen.penup()
# myPen.goto(topLeft_x, topLeft_y-row*intDim)
# myPen.pendown()
# myPen.goto(topLeft_x+9*intDim, topLeft_y-row*intDim)
# for col in digitIndices:
# if (col % 3) == 0:
# myPen.pensize(3)
# else:
# myPen.pensize(1)
# myPen.penup()
# myPen.goto(topLeft_x+col*intDim, topLeft_y)
# myPen.pendown()
# myPen.goto(topLeft_x+col*intDim, topLeft_y-9*intDim)
# for row in digitIndices:
# for col in digitIndices:
# if grid[row][col] != 0:
# text(grid[row][col], topLeft_x+col*intDim +
# 9, topLeft_y-row*intDim-intDim+8, 18)
# A function to check if the grid is full
def checkGrid(grid):
for row in digitIndices:
for col in digitIndices:
if len(grid[row][col]) != 1:
return False
# We have a complete grid!
return True
def getColumn(col, grid):
return [row[col] for row in grid]
# boxRow = [0, 0, 0, 3, 3, 3, 6, 6, 6]
# boxColumn = [0, 0, 0, 1, 1, 1, 2, 2, 2]
start = [0, 0, 0, 3, 3, 3, 6, 6, 6]
def getBox(row, col, grid):
startRow = start[row]
startColumn = start[col]
box = []
for row2 in range(startRow, startRow + 3):
for col2 in range(startColumn, startColumn + 3):
box.append(grid[row2][col2])
return box
def getBoxI(index, grid):
startRow = index // 3
startColumn = index % 3
box = []
for row2 in range(startRow, startRow + 3):
for col2 in range(startColumn, startColumn + 3):
box.append(grid[row2][col2])
return box
def groupHas(group, digit):
for cell in group:
if len(cell) == 1 and cell[0] == digit:
return True
return False
def _affects(row, col):
total = []
for i in range(9):
if i != row:
total.append((i, col))
if i // 3 == row // 3:
for j in range(9):
if j != col and j // 3 == col // 3: # And i != row
total.append((i, j))
if i != col:
total.append((row, i))
return total
affects = []
for i in range(9):
affects.append([])
for j in range(9):
affects[i].append(_affects(i, j))
def hiddenSingleCheck(group, digit):
has = [cell for cell in group if digit in cell]
if len(has) == 1:
has[0].clear()
has[0].append(digit)
return True
return False
def isHiddenSingle(row, col, grid):
if len(grid[row][col]) == 1:
return False
for group in (grid[row], getColumn(col, grid), getBox(row, col, grid)):
possible = grid[row][col][:]
for cell in group:
if cell is not grid[row][col]:
for digit in cell:
if digit in possible:
possible.remove(digit)
if possible:
return possible
return False
def isEliminated(row, col, digit, grid):
for group in (grid[row], getColumn(col, grid), getBox(row, col, grid)):
for cell in group:
if cell is not grid[row][col]:
if len(cell) == 1 and cell[0] == digit:
return True
return False
def eliminations(grid):
do = True
done = []
solved = {
"row": [[], [], [], [], [], [], [], [], []],
"col": [[], [], [], [], [], [], [], [], []],
"box": [[], [], [], [], [], [], [], [], []],
}
def len1(cell, row, col):
digit = cell[0]
if not digit in solved["row"][row]:
solved["row"][row].append(digit)
if not digit in solved["col"][col]:
solved["col"][col].append(digit)
if not digit in solved["box"][int(start[row] + start[col]/3)]:
solved["box"][int(start[row] + start[col]/3)].append(digit)
if not cell in done:
done.append(cell)
for row2, col2 in affects[row][col]:
cell2 = grid[row2][col2]
if digit in cell2:
cell2.remove(digit)
if len(cell2) == 1:
len1(cell2, row2, col2)
do = True
while do:
do = False
for row in range(9):
for col in range(9):
cell = grid[row][col]
if len(cell) == 1:
len1(cell, row, col)
if not cell in done:
result = isHiddenSingle(row, col, grid)
if result:
done.append(grid[row][col])
grid[row][col] = result
do = True
# for digit in digits:
# for i in range(9):
# condition = (
# (not (digit in solved["row"][i]) and hiddenSingleCheck(grid[i], digit)) or
# (not (digit in solved["col"][i]) and hiddenSingleCheck(getColumn(i, grid), digit)) or
# (not (digit in solved["box"][i]) and hiddenSingleCheck(getBoxI(i, grid), digit))
# )
# if condition:
# do = True
return grid
# A backtracking/recursive function to check all possible combinations of numbers until a solution is found
def solutionCount(grid, depth=1, start=0):
if depth > 81:
printGrid(grid)
raise ValueError("too much depth")
grid = eliminations(grid)
numberOfSolutions = 0
# Find next empty cell
for i in range(start, 81):
row = i // 9
col = i % 9
# When there is an empty cell, add up the possibilities for each empty cell,
# and return
if len(grid[row][col]) != 1:
for digit in grid[row][col]:
# Check that this value has not already be used on this row, column, or box
if not groupHas(grid[row], digit):
if not groupHas(getColumn(col, grid), digit):
if not groupHas(getBox(row, col, grid), digit):
copy = copyGrid(grid)
copy[row][col] = [digit]
numberOfSolutions += solutionCount(copy, depth+1, i)
# early exit on multiple solutions
# remove this if you actually need an accurate solution count
# if numberOfSolutions > 1:
# return numberOfSolutions + 1e7
return numberOfSolutions
# No empty cells, so 1 solution
return 1
# Generates a random filled grid
# Almost the same as solvegrid
def fillGrid(grid, start=0):
for i in range(start, 81):
row = i // 9
col = i % 9
if len(grid[row][col]) > 1:
randomDigits = digits[:] # Different
shuffle(randomDigits) # Different
for digit in randomDigits: # Different
if not groupHas(grid[row], digit):
if not groupHas(getColumn(col, grid), digit):
if not groupHas(getBox(row, col, grid), digit):
grid[row][col] = [digit]
if fillGrid(grid, i): # Different
return True
grid[row][col] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Couldn't place a digit
return False
# No more empty cells!
return True
def printGrid(grid, simple=False):
gridstr = ""
for row in grid:
for cell in row:
if simple:
gridstr += str(cell[0])
else:
for digit in digits:
if digit in cell:
gridstr += str(digit)
else:
gridstr += "0"
gridstr += " "
gridstr += "\n"
print(gridstr)
def copyGrid(grid):
copy = []
for r in digitIndices:
copy.append([])
for c in digitIndices:
copy[r].append(grid[r][c][:])
return copy
# Generate a Fully Solved Grid
fillGrid(grid)
printGrid(grid, True)
# drawGrid(grid)
# myPen.getscreen().update()
sleep(1)
# Start adding candidates one by one
success = True
numLeft = 729 - 81
fails = []
notdone = []
for row in digitIndices:
for col in digitIndices:
for value in digits:
notdone.append((row, col, value))
while success:
# if time.time_ns() - starttime > 1_000_000_000_000:
# printGrid(grid)
# raise ValueError("too slow")
successes = []
# Optimization: When there's only 17 left, you can't remove any more
if numLeft > 17:
# Loop over all digits and find the ones that can be taken off
for hmm in notdone:
row, col, value = hmm
if value in grid[row][col]:
notdone.remove(hmm)
else:
# Take a full copy of the grid
copy = copyGrid(grid)
copy[row][col].append(value)
# Optimization: Don't check solutionCount for first eight
result = bool(isHiddenSingle(row, col, copy)) or isEliminated(row, col, digit, copy) or solutionCount(copy) if numLeft < 729 - 81 - 8 else 1
if result == 1:
successes.append(hmm)
#print(numLeft, row, col, value, "SUCCESS")
elif result == 0:
printGrid(copy)
print("vs")
printGrid(grid)
copy = copyGrid(grid)
copy[row][col].append(value)
def eliminations(grid):
return grid
print("vs")
print(solutionCount(copy))
print(copy)
raise ValueError(result)
else:
notdone.remove(hmm)
fails.append(hmm)
print(numLeft, row, col, value, len(notdone), "FAIL", result)
if len(successes) == 0:
success = False
else:
hmm = random.choice(successes)
notdone.remove(hmm)
row, col, digit = hmm
cell = grid[row][col]
for i in range(len(cell) + 1):
if i == len(cell):
cell.append(digit)
elif cell[i] > digit:
cell.insert(i, digit)
break
numLeft -= 1
print(numLeft, row, col, digit, len(notdone), "ADDED")
# myPen.clear()
# drawGrid(grid)
# myPen.getscreen().update()
print(grid)
printGrid(grid)
finishtime = time.time_ns()
timetaken = finishtime - starttime
print(f"{timetaken / 1_000_000_000}s")
input("Sudoku Grid Ready. Press enter to finish")