-
Notifications
You must be signed in to change notification settings - Fork 4
/
solver.go
69 lines (58 loc) · 2.08 KB
/
solver.go
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
package solver
import "github.com/dimchansky/dlx"
// SolveFirst finds first solution of Sudoku in 81 character string format.
func SolveFirst(s string) (res string, ok bool) {
Solve(s, SolutionAccepterFunc(func(solution string) bool {
ok = true
res = solution
return true
}))
return
}
// SolutionAccepter accepts solutions for the given Sudoku in 81 character string format.
// Each solution is passed to AcceptSolution. Solver stops immediately when AcceptSolution returns true.
type SolutionAccepter interface {
AcceptSolution(solution string) bool
}
// The SolutionAccepterFunc type is an adapter to allow the use of
// ordinary functions as SolutionAccepter. If f is a function with the appropriate signature,
// SolutionAccepterFunc(f) is a SolutionAccepter that calls f.
type SolutionAccepterFunc func(string) bool
// AcceptSolution calls f(exactCover) and returns its result.
func (f SolutionAccepterFunc) AcceptSolution(solution string) bool {
return f(solution)
}
// Solve finds all solutions of Sudoku in 81 character string format.
// Each solution is passed to accepter. It stops immediately when accepter AcceptSolution returns true.
func Solve(s string, accepter SolutionAccepter) {
m := encodeConstraints(s)
m.Solve(dlx.SolutionAccepterFunc(func(cs [][]int) bool {
return accepter.AcceptSolution(decodeExactCoverSolution(cs))
}))
}
func encodeConstraints(s string) dlx.Matrix {
m := dlx.NewMatrix(324)
for row, position := 0, 0; row < 9; row++ {
for column := 0; column < 9; column, position = column+1, position+1 {
region := row/3*3 + column/3
digit := int(s[position] - '1') // zero based digit
if digit >= 0 && digit < 9 {
m.AddRow(position, 81+row*9+digit, 162+column*9+digit, 243+region*9+digit)
} else {
for digit = 0; digit < 9; digit++ {
m.AddRow(position, 81+row*9+digit, 162+column*9+digit, 243+region*9+digit)
}
}
}
}
return m
}
func decodeExactCoverSolution(cs [][]int) string {
b := make([]byte, len(cs))
for _, row := range cs {
position := row[0]
value := row[1] % 9
b[position] = byte(value) + '1'
}
return string(b)
}