-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
214 lines (180 loc) · 4.63 KB
/
grid.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
package models
import (
"fmt"
)
type DigitsPositionsMasks struct {
Rows [][]uint16
Cols [][]uint16
Boxes [][]uint16
}
type Grid struct {
Grid []Cell
Rows [][]*Cell
Cols [][]*Cell
Boxes [][]*Cell
DigitsPositions DigitsPositionsMasks
}
func NewGrid(s string) (*Grid, error) {
if len(s) != 81 {
return nil, fmt.Errorf("error parsing input grid string, wrong size : %s", s)
}
g := newEmptyGrid()
// Fill the grid
for i,v := range s {
if v >= '1' && v <= '9' {
g.InsertNumberByIndex(int(v - '1' + 1),i)
continue
} else if v != '.' {
return nil, fmt.Errorf("error parsing input grid string, unsupported character : %s", s)
}
}
return g, nil
}
func (g Grid) IsValid() bool {
for i := 0; i < 9; i++ {
ok := isSetValid(g.Rows[i])
if !ok {
fmt.Printf("Row %d\n",i+1)
return false
}
ok = isSetValid(g.Cols[i])
if !ok {
fmt.Printf("Col %d\n",i+1)
return false
}
ok = isSetValid(g.Boxes[i])
if !ok {
fmt.Printf("Box %d\n",i+1)
return false
}
}
for i := 0; i < 81;i++ {
if g.Grid[i].HasNoCandidate() {
r,c := IndexToCoord(i)
fmt.Printf("r%dc%d has no candidates\n",r+1,c+1)
return false
}
}
return true
}
func (g Grid) IsFilled() bool {
for _,v := range g.Grid {
if !v.IsSet() {
return false
}
}
return true
}
func isSetValid(set []*Cell) bool {
present := make([]bool,9)
for i:= 0; i < 9; i++ {
n := set[i].val
if n != 0 {
if present[n-1] {
return false
}
present[n-1] = true
}
}
return true
}
func (g *Grid) InsertNumberByIndex(n int, index int) {
row, col := IndexToCoord(index)
g.InsertNumberByCoord(n,row,col)
}
func (g *Grid) InsertNumberByCoord(n int, row int , col int) {
g.Rows[row][col].setValue(n)
box := CoordToBox(row,col)
for i := 0; i < 9; i++ {
g.Rows[row][i].RemoveCandidate(n)
g.Cols[col][i].RemoveCandidate(n)
g.Boxes[box][i].RemoveCandidate(n)
}
}
// UpdateDigitsPositionMask creates a map with 3 keys (rows,cols,boxes)
// each element contains a 2D array of uint16. The first dimension correspond to the set number (row 1, col3...)
// the second dimension correspond to a sudoku number (base 0) for witch you want the mask in the row
// ex: row[0][4] = 0b100100100 means that in row 1, the 5 could be in positions 3,6 or 9
func (g * Grid) UpdateDigitsPositionMask() {
for c := 1; c <= 9; c++ {
var mask uint16 = 1 << (c - 1)
for i := 0; i < 9; i++ {
g.DigitsPositions.Rows[i][c-1] = 0
g.DigitsPositions.Cols[i][c-1] = 0
g.DigitsPositions.Boxes[i][c-1] = 0
for j := 0; j < 9; j++ {
cBitRow := mask & g.Rows[i][j].GetCandidates()
cBitCol := mask & g.Cols[i][j].GetCandidates()
cBitBox := mask & g.Boxes[i][j].GetCandidates()
if j-(c-1) < 0 {
g.DigitsPositions.Rows[i][c-1] = g.DigitsPositions.Rows[i][c-1] | (cBitRow >> (c - 1 - j))
g.DigitsPositions.Cols[i][c-1] = g.DigitsPositions.Cols[i][c-1] | (cBitCol >> (c - 1 - j))
g.DigitsPositions.Boxes[i][c-1] = g.DigitsPositions.Boxes[i][c-1] | (cBitBox >> (c - 1 - j))
} else {
g.DigitsPositions.Rows[i][c-1] = g.DigitsPositions.Rows[i][c-1] | (cBitRow << (j - c + 1))
g.DigitsPositions.Cols[i][c-1] = g.DigitsPositions.Cols[i][c-1] | (cBitCol << (j - c + 1))
g.DigitsPositions.Boxes[i][c-1] = g.DigitsPositions.Boxes[i][c-1] | (cBitBox << (j - c + 1))
}
}
}
}
}
func newEmptyGrid() *Grid {
g := &Grid{}
g.Grid = make([]Cell, 81)
for i := range g.Grid {
g.Grid[i] = NewCell()
}
g.Rows = make([][]*Cell,9)
g.Cols = make([][]*Cell,9)
g.Boxes = make([][]*Cell,9)
g.DigitsPositions.Rows = make([][]uint16, 9)
g.DigitsPositions.Cols = make([][]uint16, 9)
g.DigitsPositions.Boxes = make([][]uint16, 9)
for i := 0; i < 9; i++ {
g.Rows[i] = make([]*Cell, 9)
g.Cols[i] = make([]*Cell, 9)
g.Boxes[i] = make([]*Cell, 9)
g.DigitsPositions.Rows[i] = make([]uint16, 9)
g.DigitsPositions.Cols[i] = make([]uint16, 9)
g.DigitsPositions.Boxes[i] = make([]uint16, 9)
for j := 0; j < 9; j++ {
g.Rows[i][j] = &(g.Grid[i*9 + j])
g.Cols[i][j] = &(g.Grid[i + j*9])
g.Boxes[i][j] = &(g.Grid[j%3 + (j/3)*9 + 3*i + (i/3)*18])
}
}
return g
}
func (g Grid) Print() {
fmt.Print("|-----------------------------|\n")
for i := 0; i < 9; i++ {
fmt.Print("|")
for j := 0 ; j < 9; j++ {
v := g.Grid[9*i+j].val
if v != 0 {
fmt.Print(" ",v," ")
} else {
fmt.Print(" ")
}
if (j+1) %3 == 0 {
fmt.Print("|")
}
}
fmt.Print("\n")
if (i + 1)%3 == 0{
fmt.Print("|-----------------------------|\n")
}
}
}
func (g Grid) String() string{
bytes := make([]byte,81)
for i,v := range g.Grid {
if v.val == 0 {
bytes[i] = '.'
} else {
bytes[i] = byte(v.val) +'0'
}
}
return string(bytes)
}