-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
240 lines (205 loc) · 3.84 KB
/
main.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
type Sudoku [9][9]int64
func (s *Sudoku) Clone() Sudoku {
var clone Sudoku
for i := 0; i < 9; i++ {
copy(clone[i][:], s[i][:])
}
return clone
}
func (s Sudoku) String() string {
var sb strings.Builder
for i, row := range s {
for j, val := range row {
if val == 0 {
sb.WriteString(".")
} else {
sb.WriteString(fmt.Sprintf("%d", val))
}
if j == 2 || j == 5 {
sb.WriteString(" ")
}
if j < 8 {
sb.WriteString(" ")
}
}
sb.WriteString("\n")
if (i+1)%3 == 0 && i < 8 {
sb.WriteString("\n")
}
}
return sb.String()
}
func (s *Sudoku) IsSolved() bool {
for i := range 9 {
for j := range 9 {
if s[i][j] == 0 {
return false
}
}
}
return true
}
func (s Sudoku) GetPosibilities(i, j int) []int64 {
if s[i][j] != 0 {
return []int64{s[i][j]}
}
posibilities := map[int64]struct{}{}
for i := int64(1); i < 10; i++ {
posibilities[i] = struct{}{}
}
// Remove row nums
for row := range 9 {
v := s[row][j]
if v != 0 {
delete(posibilities, v)
}
}
// Remove row columns
for col := range 9 {
v := s[i][col]
if v != 0 {
delete(posibilities, v)
}
}
// Remove numbers in the 3x3 cell
boxRowStart := (i / 3) * 3
boxColStart := (j / 3) * 3
for row := boxRowStart; row < boxRowStart+3; row++ {
for col := boxColStart; col < boxColStart+3; col++ {
v := s[row][col]
if v != 0 {
delete(posibilities, v)
}
}
}
var result []int64
for k := int64(1); k < 10; k++ {
if _, ok := posibilities[k]; ok {
result = append(result, k)
}
}
return result
}
func (s Sudoku) SatisfyConstraints() Sudoku {
outerLoop:
for {
c := int64(0)
for i := range 9 {
for j := range 9 {
if s[i][j] == 0 {
posibilities := s.GetPosibilities(i, j)
if len(posibilities) == 1 {
s[i][j] = posibilities[0]
c++
}
}
}
}
if c == 0 {
break outerLoop
}
}
return s
}
func (s Sudoku) Solve() (Sudoku, bool) {
if s.IsSolved() {
return s, true
}
s = s.SatisfyConstraints()
if s.IsSolved() {
return s, true
}
var row, col int
found := false
outerLoop:
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
if s[i][j] == 0 {
row, col = i, j
found = true
break outerLoop
}
}
}
if !found {
return s, true
}
possibilities := s.GetPosibilities(row, col)
for _, val := range possibilities {
newS := s.Clone()
newS[row][col] = val
solution, solved := newS.Solve()
if solved {
return solution, solved
}
}
return s, s.IsSolved()
}
func ParseGame(input []string) Sudoku {
game := Sudoku{}
for i := range 9 {
for j := range 9 {
val, err := strconv.ParseInt(string(input[i][j]), 10, 64)
if err != nil {
panic(err)
}
game[i][j] = val
}
}
return game
}
func main() {
start := time.Now()
games := map[string]Sudoku{}
f, err := os.Open("./p096_sudoku.txt")
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(f)
lineCount := 0
var gameName string
gameStr := []string{}
for scanner.Scan() {
if lineCount == 0 {
gameName = strings.TrimSpace(scanner.Text())
} else {
gameStr = append(gameStr, strings.TrimSpace(scanner.Text()))
}
if lineCount == 9 {
game := ParseGame(gameStr)
games[gameName] = game
gameStr = []string{}
lineCount = 0
continue
}
lineCount += 1
}
err = scanner.Err()
if err != nil {
panic(err)
}
solutions := map[string]Sudoku{}
for gameName, game := range games {
solution, solved := game.Solve()
if !solved {
panic(fmt.Sprintf("game %v not solved!", gameName))
}
solutions[gameName] = solution
}
sum := int64(0)
for _, solution := range solutions {
sum += solution[0][0]*100 + solution[0][1]*10 + solution[0][2]
}
template := "Sum of 3-digit numbers at top left = %v (elapsed = %v)\n"
elapsed := time.Since(start)
fmt.Printf(template, sum, elapsed)
}