-
Notifications
You must be signed in to change notification settings - Fork 155
/
sudoku_solver.go
181 lines (164 loc) Β· 3.99 KB
/
sudoku_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
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
package main
import (
"errors"
"fmt"
"math"
"os"
"strconv"
"time"
)
func main() {
board := Board{Size: 9}
puzzle := "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
if len(os.Args) > 1 {
args := os.Args[1:]
if len(args) > 0 && args[0] != "" {
puzzle = args[0]
}
}
fmt.Println(puzzle)
_, err := board.Generate(puzzle)
if err != nil {
fmt.Println(err)
return
}
board.Print()
// fmt.Println(board.InBox(1, 1, 7))
start := time.Now()
isSolved := board.Solve()
if isSolved {
fmt.Println("Board is solved π")
} else {
fmt.Println("Board is not solved π")
}
board.Print()
t := time.Now()
elapsed := t.Sub(start)
fmt.Printf("Toke %v secs to solve.\n", elapsed.Seconds())
// board.PrintPossibles()
}
type Number struct {
Solved bool
Possibles []int
Number int
PosX int
PosY int
}
type Board struct {
Size int
Numbers []Number
Value string
}
func (board *Board) Generate(value string) (bool, error) {
mod := math.Mod(float64(len(value)), float64(board.Size))
if mod > 0 {
return false, errors.New("Your board value is not correct. ")
}
board.Value = value
fmt.Printf("Board Size: %v\n", board.Size)
counter := 0
for row := 0; row < board.Size; row++ {
for col := 0; col < board.Size; col++ {
pint, _ := strconv.ParseInt(value[counter:counter+1], 0, 32)
v := int(pint)
board.Numbers = append(board.Numbers, Number{Number: v, PosX: row, PosY: col, Solved: v > 0})
counter++
}
}
return true, nil
}
func (board *Board) Print() (bool, error) {
fmt.Println("βββββββββββ| BOARD |βββββββββββ")
for i := 0; i < len(board.Numbers); i++ {
if board.Numbers[i].PosY == 0 {
fmt.Print("|")
}
if board.Numbers[i].Number == 0 {
fmt.Print(" Β· ")
} else {
fmt.Printf(" %v ", board.Numbers[i].Number)
}
if int(math.Mod(float64(i+1), float64(3))) == 0 {
fmt.Print("|")
}
if int(math.Mod(float64(i+1), float64(board.Size))) == 0 {
fmt.Println()
}
if int(math.Mod(float64(i+1), float64(27))) == 0 {
fmt.Println("βββββββββββββββββββββββββββββββ")
}
}
return true, nil
}
func (number *Number) HasPossible(n int) bool {
for _, v := range number.Possibles {
if v == n {
return true
}
}
return false
}
func (board *Board) Solve() bool {
size := len(board.Numbers)
for i := 0; i < size; i++ {
number := &board.Numbers[i]
// fmt.Printf("%d. X: %d, Y:%d, V:%v, STATUS:%v\n", i, number.PosX, number.PosY, number.Number, number.Solved)
if !number.Solved {
for n := 1; n <= 9; n++ {
if board.IsSafe((number.PosX + 1), (number.PosY + 1), n) {
number.Number = n
number.Solved = true
if !number.HasPossible(n) {
number.Possibles = append(number.Possibles, n)
}
// fmt.Printf("%v is safe to put %d,%d\n", n, number.PosX, number.PosY)
if board.Solve() {
return true
}
number.Number = 0
number.Solved = false
}
}
return false
}
}
fmt.Println()
return true
}
func (board *Board) IsSafe(row int, col int, number int) bool {
return !board.InRow(row, number) && !board.InColumn(col, number) && !board.InBox(row, col, number)
}
func (board *Board) InBox(row int, col int, n int) bool {
row--
col--
rowmin := row - int(math.Mod(float64(row), float64(3)))
colmin := col - int(math.Mod(float64(col), float64(3)))
rowmax := rowmin + 3
colmax := colmin + 3
for _, number := range board.Numbers {
if number.PosX >= rowmin && number.PosX < rowmax && number.PosY >= colmin && number.PosY < colmax {
if number.Number == n {
return true
}
}
}
return false
}
func (board *Board) InRow(row int, number int) bool {
row--
for _, v := range board.Numbers {
if v.PosX == row && v.Number == number {
return true
}
}
return false
}
func (board *Board) InColumn(column int, number int) bool {
column--
for _, v := range board.Numbers {
if v.PosY == column && v.Number == number {
return true
}
}
return false
}