/
sudoku.go
177 lines (154 loc) · 3.41 KB
/
sudoku.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
// Sudoku sover in Go
package main
import (
"bytes"
"fmt"
"os"
"io/ioutil"
"regexp"
"strings"
"time"
)
const BOARD_SIZE = 9 // rows and column size
// Build a slice of slices to represent the board from a string
func FromStr(boardRepr string) [][]int {
board := make([][]int, BOARD_SIZE)
for i := range board {
board[i] = make([]int, BOARD_SIZE)
}
x, y := 0, 0
for i := range boardRepr {
if boardRepr[i] >= '0' && boardRepr[i] <= '9' {
board[x][y] = int(boardRepr[i]) - 48
if y == BOARD_SIZE-1 {
x += 1 % BOARD_SIZE
y = 0
} else {
y += 1 % BOARD_SIZE
}
}
}
return board
}
// Board to string
func ToStr(board [][]int) string {
var b bytes.Buffer
for i := range board {
for j := range board[i] {
b.WriteString(fmt.Sprintf("%d ", board[i][j]))
}
b.WriteString("\n")
}
return b.String()
}
// Return the next spot where is possible to put a number
func nextEmpty(board [][]int) []int {
for i := range board {
for j := range board[i] {
if board[i][j] == 0 {
return []int{i, j}
}
}
}
return nil
}
// Return true if the number n can be put in the board at specific spot
func canPut(board [][]int, spot []int, n int) bool {
x, y := spot[0], spot[1]
for i := range board {
// test line
if board[i][y] == n {
return false
}
// test column
if board[x][i] == n {
return false
}
}
// test square
a, b := x-(x%3), y-(y%3)
for i := a; i < a+3; i++ {
for j := b; j < b+3; j++ {
if board[i][j] == n {
return false
}
}
}
return true
}
// Solve using backtracking
func solve(board [][]int) [][]int {
spot := nextEmpty(board)
if spot == nil {
return board
}
x, y := spot[0], spot[1]
for i := 1; i < 10; i++ {
if canPut(board, spot, i) {
board[x][y] = i
newBoard := solve(board)
if nextEmpty(newBoard) == nil {
// solution found
return newBoard
}
}
}
// solution not found, backtrack
board[x][y] = 0
return board
}
// genericSplit provides a generic version of Split and SplitAfter.
// Set the includeSep bool to true to have it include the separtor.
func genericSplit(re *regexp.Regexp,s string, numFields int, includeSep bool) []string {
if numFields == 0 {
return make([]string, 0)
}
// Using regexp, including the separator is really easy. Instead of
// including up to the start of the separator we include to the end.
// The start of the separator is stored in index 0.
// The end of the separator is stored in index 1.
var includeTo int
if includeSep {
includeTo = 1
} else {
includeTo = 0
}
count := re.FindAllStringIndex(s, numFields-1)
n := len(count) + 1
stor := make([]string, n)
if n == 1 {
stor[0] = s
return stor
}
stor[0] = s[:count[0][includeTo]]
for i := 1; i < n-1; i++ {
stor[i] = s[count[i-1][1]:count[i][includeTo]]
}
stor[n-1] = s[count[n-2][1]:]
return stor
}
func processBatch(file string) {
data, err := ioutil.ReadFile(file)
if (err != nil) {
panic(err)
}
var solvedOutput bytes.Buffer
count := 0
before := time.Now()
for _, line := range strings.Split(string(data), "\n") {
sudoku := FromStr(line)
solvedOutput.WriteString(ToStr(solve(sudoku)))
solvedOutput.WriteString("\n")
count++
}
diff := time.Now().Sub(before)
fmt.Printf("-- Solved %d sudokus. Elapsed time: %f seconds\n",count, diff.Seconds())
err = ioutil.WriteFile("solved_" + file, solvedOutput.Bytes(), 0644)
if (err != nil) {
panic(err)
}
}
func main() {
file := os.Args[1]
processBatch(file)
}