-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
113 lines (95 loc) · 1.94 KB
/
model.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
package main
type Clue []int
type Puzzle struct {
RowSize int
ColSize int
RowClues []Clue
ColClues []Clue
}
type GridType = int8
var GridTypeEnum = struct {
Blank GridType
Colored GridType
Unknown GridType
}{-1, 1, 0}
type Solution struct {
RowSize int
ColSize int
grids [][]GridType
}
type Line []GridType
func NewSolution(rowSize, colSize int) Solution {
ret := Solution{
RowSize: rowSize,
ColSize: colSize,
grids: make([][]GridType, rowSize),
}
for r := 0; r < rowSize; r++ {
row := make([]GridType, colSize, colSize)
ret.grids[r] = row
}
return ret
}
func (s *Solution) Copy() Solution {
ret := Solution{
RowSize: s.RowSize,
ColSize: s.ColSize,
grids: make([][]GridType, s.RowSize),
}
for r := 0; r < s.RowSize; r++ {
row := make([]GridType, s.ColSize, s.ColSize)
copy(row, s.grids[r])
ret.grids[r] = row
}
return ret
}
func (s *Solution) GetGrid(rIdx, cIdx int) GridType {
return s.grids[rIdx][cIdx]
}
func (s *Solution) SetGrid(rIdx, cIdx int, grid GridType) {
s.grids[rIdx][cIdx] = grid
}
func (s *Solution) GetRow(idx int) Line {
ret := make(Line, s.ColSize, s.ColSize)
for c := 0; c < s.ColSize; c++ {
ret[c] = s.grids[idx][c]
}
return ret
}
func (s *Solution) GetCol(idx int) Line {
ret := make(Line, s.RowSize, s.RowSize)
for r := 0; r < s.RowSize; r++ {
ret[r] = s.grids[r][idx]
}
return ret
}
func (s *Solution) SetRow(idx int, row Line) {
for c := 0; c < s.ColSize; c++ {
s.grids[idx][c] = row[c]
}
}
func (s *Solution) SetCol(idx int, col Line) {
for r := 0; r < s.RowSize; r++ {
s.grids[r][idx] = col[r]
}
}
func (l *Line) Union(other Line) {
for i := 0; i < len(*l); i++ {
if (*l)[i] != other[i] {
(*l)[i] = GridTypeEnum.Unknown
}
}
}
func (l *Line) Copy() Line {
ret := make(Line, len(*l), len(*l))
copy(ret, *l)
return ret
}
func (l *Line) EqualTo(other Line) bool {
for i := 0; i < len(*l); i++ {
if (*l)[i] != other[i] {
return false
}
}
return true
}