-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhst_single.go
227 lines (199 loc) · 6.21 KB
/
hst_single.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
package sudoku
import (
"fmt"
"math/rand"
"strconv"
)
type nakedSingleTechnique struct {
*basicSolveTechnique
}
type hiddenSingleTechnique struct {
*basicSolveTechnique
}
type obviousInCollectionTechnique struct {
*basicSolveTechnique
}
func (self *obviousInCollectionTechnique) humanLikelihood(step *SolveStep) float64 {
return self.difficultyHelper(1.0)
}
func (self *obviousInCollectionTechnique) Description(step *SolveStep) string {
if len(step.TargetNums) == 0 {
return ""
}
num := step.TargetNums[0]
groupName := "<NONE>"
groupNumber := 0
switch self.groupType {
case _GROUP_BLOCK:
groupName = "block"
groupNumber = step.TargetCells.Block()
case _GROUP_COL:
groupName = "column"
groupNumber = step.TargetCells.Col()
case _GROUP_ROW:
groupName = "row"
groupNumber = step.TargetCells.Row()
}
return fmt.Sprintf("%s is the only cell in %s %d that is unfilled, and it must be %d", step.TargetCells.Description(), groupName, groupNumber, num)
}
func (self *obviousInCollectionTechnique) Candidates(grid Grid, maxResults int) []*SolveStep {
return self.candidatesHelper(self, grid, maxResults)
}
func (self *obviousInCollectionTechnique) find(grid Grid, coordinator findCoordinator) {
obviousInCollection(grid, self, self.getter(grid), coordinator)
}
func obviousInCollection(grid Grid, technique SolveTechnique, collectionGetter func(index int) CellSlice, coordinator findCoordinator) {
indexes := rand.Perm(DIM)
for _, index := range indexes {
if coordinator.shouldExitEarly() {
return
}
collection := collectionGetter(index)
openCells := collection.FilterByHasPossibilities()
if len(openCells) == 1 {
//Okay, only one cell in this collection has an opening, which must mean it has one possibilty.
cell := openCells[0]
possibilities := cell.Possibilities()
//len(possibiltiies) SHOULD be 1, but check just in case.
if len(possibilities) == 1 {
possibility := possibilities[0]
step := &SolveStep{
Technique: technique,
TargetCells: CellRefSlice{cell.Reference()},
TargetNums: IntSlice{possibility},
PointerCells: collection.RemoveCells(CellSlice{cell}).CellReferenceSlice(),
}
if step.IsUseful(grid) {
if coordinator.foundResult(step) {
return
}
}
}
}
}
}
func (self *nakedSingleTechnique) humanLikelihood(step *SolveStep) float64 {
return self.difficultyHelper(40.0)
}
func (self *nakedSingleTechnique) Description(step *SolveStep) string {
if len(step.TargetNums) == 0 {
return ""
}
num := step.TargetNums[0]
return fmt.Sprintf("%d is the only remaining valid number for that cell", num)
}
func (self *nakedSingleTechnique) Candidates(grid Grid, maxResults int) []*SolveStep {
return self.candidatesHelper(self, grid, maxResults)
}
func (self *nakedSingleTechnique) find(grid Grid, coordinator findCoordinator) {
//TODO: test that this will find multiple if they exist.
getter := grid.queue().NewGetter()
for {
if coordinator.shouldExitEarly() {
return
}
obj := getter.GetSmallerThan(2)
if obj == nil {
//There weren't any cells with one option left.
//If there weren't any, period, then results is still nil already.
return
}
cell := obj.(Cell)
step := &SolveStep{
Technique: self,
TargetCells: CellRefSlice{cell.Reference()},
TargetNums: IntSlice{cell.implicitNumber()},
PointerCells: cell.Neighbors().FilterByFilled().CellReferenceSlice(),
}
if step.IsUseful(grid) {
if coordinator.foundResult(step) {
return
}
}
}
}
func (self *hiddenSingleTechnique) humanLikelihood(step *SolveStep) float64 {
return self.difficultyHelper(18.0)
}
func (self *hiddenSingleTechnique) Description(step *SolveStep) string {
//TODO: format the text to say "first/second/third/etc"
if len(step.TargetCells) == 0 || len(step.TargetNums) == 0 {
return ""
}
cell := step.TargetCells[0]
num := step.TargetNums[0]
var groupName string
var otherGroupName string
var groupNum int
var otherGroupNum string
switch self.groupType {
case _GROUP_BLOCK:
groupName = "block"
otherGroupName = "cell"
groupNum = step.TargetCells.Block()
otherGroupNum = step.TargetCells.Description()
case _GROUP_ROW:
groupName = "row"
otherGroupName = "column"
groupNum = step.TargetCells.Row()
otherGroupNum = strconv.Itoa(cell.Col)
case _GROUP_COL:
groupName = "column"
otherGroupName = "row"
groupNum = step.TargetCells.Col()
otherGroupNum = strconv.Itoa(cell.Row)
default:
groupName = "<NONE>"
otherGroupName = "<NONE>"
groupNum = -1
otherGroupNum = "<NONE>"
}
return fmt.Sprintf("%d is required in the %d %s, and %s is the only %s it fits", num, groupNum, groupName, otherGroupNum, otherGroupName)
}
func (self *hiddenSingleTechnique) Candidates(grid Grid, maxResults int) []*SolveStep {
return self.candidatesHelper(self, grid, maxResults)
}
func (self *hiddenSingleTechnique) find(grid Grid, coordinator findCoordinator) {
//TODO: test that if there are multiple we find them both.
necessaryInCollection(grid, self, self.getter(grid), coordinator)
}
func necessaryInCollection(grid Grid, technique SolveTechnique, collectionGetter func(index int) CellSlice, coordinator findCoordinator) {
//This will be a random item
indexes := rand.Perm(DIM)
for _, i := range indexes {
if coordinator.shouldExitEarly() {
return
}
seenInCollection := make([]int, DIM)
collection := collectionGetter(i)
for _, cell := range collection {
for _, possibility := range cell.Possibilities() {
seenInCollection[possibility-1]++
}
}
seenIndexes := rand.Perm(DIM)
for _, index := range seenIndexes {
seen := seenInCollection[index]
if seen == 1 {
//Okay, we know our target number. Which cell was it?
for _, cell := range collection {
if cell.Possible(index + 1) {
//Found it... just make sure it's useful (it would be rare for it to not be).
step := &SolveStep{
Technique: technique,
TargetCells: CellRefSlice{cell.Reference()},
TargetNums: IntSlice{index + 1},
PointerCells: collection.FilterByUnfilled().RemoveCells(CellSlice{cell}).CellReferenceSlice(),
}
if step.IsUseful(grid) {
if coordinator.foundResult(step) {
return
}
}
//Hmm, wasn't useful. Keep trying...
}
}
}
}
}
}