This repository has been archived by the owner on Feb 18, 2018. It is now read-only.
/
board.go
257 lines (218 loc) · 4.93 KB
/
board.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
)
type board struct {
solved [81]uint
blits [81]uint
loading bool
changed bool
showSteps bool
countSolutions bool
solutionCount int
maxSolutions int
difficulty int
}
type coords struct {
row int
col int
box int
pos int
}
type inspector func(int, int) error
type containerOperator func(int, inspector) error
func getCoords(pos int) coords {
boxRow := ((pos / 9) / 3)
boxCol := ((pos % 9) / 3)
box := boxRow*3 + boxCol
return coords{row: pos / 9, col: pos % 9, box: box, pos: pos}
}
func (c coords) String() string {
return fmt.Sprintf("%c%c", getTextRow(c.row), getTextCol(c.col))
}
func getTextRow(row int) int {
if row == 8 {
return 'J' // the letter "I" is skipped
}
return 'A' + row
}
func getTextCol(col int) int {
return '1' + col
}
func (b *board) operateOnRow(pos int, op inspector) error {
startRow := (pos / 9) * 9
for r := startRow; r < startRow+9; r++ {
if err := op(r, pos); err != nil {
return err
}
}
return nil
}
func (b *board) operateOnColumn(pos int, op inspector) error {
for c := pos % 9; c < 81; c += 9 {
if err := op(c, pos); err != nil {
return err
}
}
return nil
}
func (b *board) operateOnBox(pos int, op inspector) error {
startRow := ((pos / 9) / 3) * 3
startCol := ((pos % 9) / 3) * 3
for r := startRow; r < startRow+3; r++ {
for c := startCol; c < startCol+3; c++ {
target := r*9 + c
if err := op(target, pos); err != nil {
return err
}
}
}
return nil
}
func (b *board) operateOnRCB(pos int, op inspector) error {
if err := b.operateOnRow(pos, op); err != nil {
return err
}
if err := b.operateOnColumn(pos, op); err != nil {
return err
}
if err := b.operateOnBox(pos, op); err != nil {
return err
}
return nil
}
func (b *board) operateOnCommon(pos1 int, pos2 int, op inspector) error {
coords1 := getCoords(pos1)
coords2 := getCoords(pos2)
if coords1.row == coords2.row {
if err := b.operateOnRow(pos1, op); err != nil {
return err
}
}
if coords1.col == coords2.col {
if err := b.operateOnColumn(pos1, op); err != nil {
return err
}
}
if coords1.box == coords2.box {
if err := b.operateOnBox(pos1, op); err != nil {
return err
}
}
return nil
}
func readBoard(rd io.Reader) ([]byte, error) {
r := bufio.NewReader(rd)
var line string
var err error
buf := bytes.NewBufferString("")
for i := 0; i < 9; i++ {
if line, err = r.ReadString('\n'); err != nil && err != io.EOF {
return nil, err
}
line = strings.Trim(line, "\r\n")
// small deviation from spec, accept compact 81-char format
if i == 0 && len(line) == 81 {
return []byte(line), nil
}
if len(line) != 17 {
return nil, fmt.Errorf("line %d: expected: len=17, actual: len=%d. line: %q", i+1, len(line), line)
}
for j, r := range line {
if j%2 == 0 {
if r != '_' {
var v int
if v, err = strconv.Atoi(string(r)); err != nil || v == 0 {
return nil, fmt.Errorf("line %d, pos %d: expected: digit > 0 or '_', actual: %q. line: %q", i+1, j+1, r, line)
}
}
buf.WriteRune(r)
} else {
if r != ' ' {
return nil, fmt.Errorf("line %d, pos %d: expected: \" \", actual: %q. line: %q", i+1, j+1, r, line)
}
}
}
}
return buf.Bytes(), nil
}
func getBoard(fileName string) (*board, error) {
b, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
return loadBoard(b)
}
func loadBoard(b []byte) (*board, error) {
b = bytes.Replace(b, []byte{'\r'}, []byte{}, -1)
b = bytes.Replace(b, []byte{'\n'}, []byte{}, -1)
b = bytes.Replace(b, []byte{' '}, []byte{}, -1)
if len(b) != 81 {
return nil, fmt.Errorf("line needs to be 81 chars long. line: %q", string(b))
}
board := &board{loading: true}
for i := 0; i < 81; i++ {
board.blits[i] = 0x1FF
}
for i := 0; i < 81; i++ {
// allow _ 0 . to indicate an empty cell
if b[i] != '_' && b[i] != '0' && b[i] != '.' {
val := uint(b[i] - 48)
if err := board.SolvePosition(i, val); err != nil {
return board, err
}
}
}
board.loading = false
return board, nil
}
func (b *board) numSolved() int {
num := 0
for i := 0; i < 81; i++ {
if b.solved[i] != 0 {
num++
}
}
return num
}
func (b *board) isSolved() bool {
return b.numSolved() == 81
}
func (b *board) getVisibleCells(pos int) []int {
var list []int
coords := getCoords(pos)
for i := 0; i < 81; i++ {
if i == pos || b.solved[i] != 0 {
continue
}
t := getCoords(i)
if t.row == coords.row ||
t.col == coords.col ||
t.box == coords.box {
list = append(list, i)
}
}
return list
}
func (b *board) getVisibleCellsWithHint(pos int, hint uint) []int {
var list []int
coords := getCoords(pos)
for i := 0; i < 81; i++ {
if i == pos || b.solved[i] != 0 || b.blits[i]&hint != hint {
continue
}
t := getCoords(i)
if t.row == coords.row ||
t.col == coords.col ||
t.box == coords.box {
list = append(list, i)
}
}
return list
}