-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_solver.jl
108 lines (90 loc) · 2.41 KB
/
sudoku_solver.jl
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
#!/usr/bin/julia
# Trizen
# Date: 08 February 2017
# https://github.com/trizen
# Recursive brute-force Sudoku solver.
# See also:
# https://en.wikipedia.org/wiki/Sudoku
function check(i, j)
id, im = div(i, 9), mod(i, 9)
jd, jm = div(j, 9), mod(j, 9)
jd == id && return true
jm == im && return true
div(id, 3) == div(jd, 3) &&
div(jm, 3) == div(im, 3)
end
const lookup = zeros(Bool, 81, 81)
for i in 1:81
for j in 1:81
lookup[i,j] = check(i-1, j-1)
end
end
function solve_sudoku(callback::Function, grid::Array{Int64})
(function solve()
for i in 1:81
if grid[i] == 0
t = Dict{Int64, Nothing}()
for j in 1:81
if lookup[i,j]
t[grid[j]] = nothing
end
end
for k in 1:9
if !haskey(t, k)
grid[i] = k
solve()
end
end
grid[i] = 0
return
end
end
callback(grid)
end)()
end
function display(grid)
println("Solution: ")
for i in 1:length(grid)
print(grid[i], " ")
i % 3 == 0 && print(" ")
i % 9 == 0 && print("\n")
i % 27 == 0 && print("\n")
end
end
sudoku_board =
[2, 0, 0, 0, 7, 0, 0, 0, 3,
1, 0, 0, 0, 0, 0, 0, 8, 0,
0, 0, 4, 2, 0, 9, 0, 0, 5,
9, 4, 0, 0, 0, 0, 6, 0, 8,
0, 0, 0, 8, 0, 0, 0, 9, 0,
0, 0, 0, 0, 0, 0, 0, 7, 0,
7, 2, 1, 9, 0, 8, 0, 6, 0,
0, 3, 0, 0, 2, 7, 1, 0, 0,
4, 0, 0, 0, 0, 3, 0, 0, 0]
if false
sudoku_board = [
0, 0, 0, 8, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 4, 3,
5, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 7, 0, 8, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 2, 0, 0, 3, 0, 0, 0, 0,
6, 0, 0, 0, 0, 0, 0, 7, 5,
0, 0, 3, 4, 0, 0, 0, 0, 0,
0, 0, 0, 2, 0, 0, 6, 0, 0
]
end
if false
sudoku_board = [
8, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 3, 6, 0, 0, 0, 0, 0,
0, 7, 0, 0, 9, 0, 2, 0, 0,
0, 5, 0, 0, 0, 7, 0, 0, 0,
0, 0, 0, 0, 4, 5, 7, 0, 0,
0, 0, 0, 1, 0, 0, 0, 3, 0,
0, 0, 1, 0, 0, 0, 0, 6, 8,
0, 0, 8, 5, 0, 0, 0, 1, 0,
0, 9, 0, 0, 0, 0, 4, 0, 0
]
end
solve_sudoku(display, sudoku_board)