-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (52 loc) · 1.91 KB
/
Copy pathmain.py
File metadata and controls
68 lines (52 loc) · 1.91 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
"""Solving sudokus."""
import numpy as np
from grid import Grid
def branchOut(grid, i, j, values):
"""Branch out between the values and return newly-solved grids."""
_ret = []
for val in values:
_grid = Grid(grid.n)
_grid.copy(grid)
_grid.propagate(i, j, val)
_grid.solve()
_ret += [_grid]
return _ret
def createUniverses(grid, n_options=2):
"""For each multi-option case, try each option in parallel."""
# Get changes for n=2, if stuck then continue to n=3 etc..
# Continue until one change is implemented, then return to let n=2 work
x = [-1]
while len(x) > 0:
x, y, z = grid.getChanges(n_options)
for i, j, values in zip(x, y, z):
grids = branchOut(grid, i, j, values)
grids_solved = np.array(list(map(lambda x: x.isSolved(), grids)),
dtype=int)
grids_valid = np.array(list(map(lambda x: x.isValid(), grids)),
dtype=int)
# If one is solved and valid, return
combination = grids_solved + grids_valid
where_combination = np.where(combination == 2)
if len(where_combination[0]) > 0:
return grids[where_combination[0][0]]
# If only one is valid, return
if np.sum(grids_valid) == 1:
return grids[np.where(grids_valid)[0][0]]
n_options += 1
def main(nGrid=9):
"""Load a sudoku grid then solve it."""
g = Grid(nGrid)
g.loadStd()
print("Initial grid:")
print(g, "\n----------------------------")
# Try to solve directly
g.solve()
if g.isSolved(): return
# Solve with branching
while not g.isSolved():
newGrid = createUniverses(g)
g.copy(newGrid)
print("Solution:")
print(g)
if __name__ == '__main__':
main()