-
Notifications
You must be signed in to change notification settings - Fork 1
/
hst_guess.go
82 lines (64 loc) · 2.18 KB
/
hst_guess.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
package sudoku
import (
"fmt"
"math"
)
type guessTechnique struct {
*basicSolveTechnique
}
func (self *guessTechnique) humanLikelihood(step *SolveStep) float64 {
//Guess is so horribly terrible that we want it to NEVER come in lower
//than even a chain of cullSteps. Making it postive infinity means that no
//twiddles will ever make it less impossibly bad.
return self.difficultyHelper(math.Inf(1))
}
func (self *guessTechnique) Description(step *SolveStep) string {
return fmt.Sprintf("we have no other moves to make, so we randomly pick a cell with the smallest number of possibilities, %s, and pick one of its possibilities", step.TargetCells.Description())
}
func (self *guessTechnique) Candidates(grid Grid, maxResults int) []*SolveStep {
return self.candidatesHelper(self, grid, maxResults)
}
func (self *guessTechnique) find(grid Grid, coordinator findCoordinator) {
//We used to have a very elaborate aparatus for guess logic where we'd
//earnestly guess and then HumanSolve forward until we discovered a
//solution or an inconsistency. But that was dumb because we never
//returned a result down a wrong guess (or otherwise proved that we had
//done the 'real' work). And it massively complicated the flow. So... just
//cheat. Brute force solve the grid, pick a cell with small number of
//possibilities, and then just immediately return the correct value for
//it. Done!
solvedGrid := grid.MutableCopy()
solvedGrid.Solve()
if !solvedGrid.Solved() {
return
}
getter := grid.queue().NewGetter()
for {
if coordinator.shouldExitEarly() {
return
}
obj := getter.Get()
if obj == nil {
break
}
//This WILL happen, since guess will return a bunch of possible guesses you could make.
if obj.rank() > 3 {
//Given that this WILL happen, it's important to return results so far, whatever they are.
break
}
//Convert RankedObject to a cell
cell := obj.(Cell)
cellInSolvedGrid := cell.InGrid(solvedGrid)
num := cellInSolvedGrid.Number()
step := &SolveStep{
Technique: self,
TargetCells: CellRefSlice{cell.Reference()},
TargetNums: IntSlice{num},
}
if step.IsUseful(grid) {
if coordinator.foundResult(step) {
return
}
}
}
}