-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.go
280 lines (256 loc) · 6.97 KB
/
puzzle.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package sudoku
import (
"errors"
"fmt"
"math"
"sync"
"time"
)
var (
// ErrNoMoreMoves is returned when there are no available moves on the current iteration.
ErrNoMoreMoves = errors.New("no more moves")
// ErrMissingIteration is returned when a required iteration is missing.
ErrMissingIteration = errors.New("missing iteration")
// ErrInvalidPuzzleSize is returned when the puzzle size or section size cannot be calculated.
ErrInvalidPuzzleSize = errors.New("invalid puzzle size")
)
// CompletionRate contains information on a puzzles completion.
type CompletionRate struct {
Completed bool
Failed bool
Error error
TotalCells int
FixedCells int
FilledCells int
AttemptedIterations int
CellIndex int
MinValueAtCell int
StartedAt time.Time
FailedAt time.Time
CompletedAt time.Time
}
// NewPuzzle returns a new puzzle.
func NewPuzzle(items []int) (*Puzzle, error) {
puzzleSize, err := CalculatePuzzleSize(items)
if err != nil {
return nil, err
}
sectionSize, err := CalculateSectionSize(items, puzzleSize)
if err != nil {
return nil, err
}
firstIteration := newIteration(items, puzzleSize, sectionSize)
return &Puzzle{
puzzleSize: puzzleSize,
iterations: []*iteration{
firstIteration,
},
lastIteration: nil,
currentIteration: firstIteration,
iterationMu: &sync.Mutex{},
errMu: &sync.Mutex{},
attemptedIterationsMu: &sync.Mutex{},
timerMu: &sync.Mutex{},
}, nil
}
// Puzzle is a sudoku puzzle.
type Puzzle struct {
iterationMu *sync.Mutex
iterations []*iteration
lastIteration *iteration
currentIteration *iteration
puzzleSize int
errMu *sync.Mutex
err error
attemptedIterationsMu *sync.Mutex
attemptedIterations int
timerMu *sync.Mutex
startedAt time.Time
failedAt time.Time
completedAt time.Time
}
// revert reverts back to the most recent applicable iteration.
func (p *Puzzle) revert(firstRevert bool) error {
if firstRevert {
p.iterationMu.Lock()
defer p.iterationMu.Unlock()
}
if p.lastIteration == nil {
return ErrMissingIteration
}
// remove the last iteration.
p.iterations = p.iterations[:len(p.iterations)-1]
iterationsLen := len(p.iterations)
if iterationsLen < 1 {
return ErrMissingIteration
}
// set the current iteration and increment the min value.
p.currentIteration = p.iterations[iterationsLen-1]
p.currentIteration.minValue++
// set the last iteration.
if iterationsLen >= 2 {
p.lastIteration = p.iterations[iterationsLen-2]
}
// if the min value is too high, revert again.
if p.currentIteration.minValue > p.puzzleSize {
return p.revert(false)
}
return nil
}
// next moves on to the next iteration.
func (p *Puzzle) next() error {
p.iterationMu.Lock()
nextIteration := p.currentIteration.iterate()
p.lastIteration = p.currentIteration
p.currentIteration = nextIteration
p.iterations = append(p.iterations, p.currentIteration)
p.iterationMu.Unlock()
return nil
}
// Solve solves the puzzle.
func (p *Puzzle) Solve() error {
p.timerMu.Lock()
p.startedAt = time.Now()
p.timerMu.Unlock()
if err := p.next(); err != nil {
p.timerMu.Lock()
p.failedAt = time.Now()
p.timerMu.Unlock()
p.errMu.Lock()
defer p.errMu.Unlock()
p.err = fmt.Errorf("initial iteration failed: %w", err)
return p.err
}
p.iterationMu.Lock()
p.currentIteration.index = 0
p.iterationMu.Unlock()
p.attemptedIterationsMu.Lock()
p.attemptedIterations++
p.attemptedIterationsMu.Unlock()
for {
p.iterationMu.Lock()
currentIteration := p.currentIteration
p.iterationMu.Unlock()
err := currentIteration.solve()
switch err {
case nil:
// we were able to find a matching value
if currentIteration.finished() {
// all cells have a value
p.timerMu.Lock()
p.completedAt = time.Now()
p.timerMu.Unlock()
return nil
}
// continue to the next index.
if err := p.next(); err != nil {
p.timerMu.Lock()
p.failedAt = time.Now()
p.timerMu.Unlock()
p.errMu.Lock()
defer p.errMu.Unlock()
p.err = fmt.Errorf("could not move to next iteration: %w", err)
return p.err
}
continue
case ErrNoMoreMoves:
// the iteration ran out of moves.
// revert back to previous iteration with an incremented min value
if err := p.revert(true); err != nil {
p.timerMu.Lock()
p.failedAt = time.Now()
p.timerMu.Unlock()
p.errMu.Lock()
defer p.errMu.Unlock()
p.err = fmt.Errorf("could not revert: %w", err)
return p.err
}
p.attemptedIterationsMu.Lock()
p.attemptedIterations++
p.attemptedIterationsMu.Unlock()
continue
default:
p.timerMu.Lock()
p.failedAt = time.Now()
p.timerMu.Unlock()
p.errMu.Lock()
defer p.errMu.Unlock()
p.err = fmt.Errorf("could not solve iteration: %w", err)
return p.err
}
}
}
// Result returns the last iteration.
func (p *Puzzle) Result() ([]int, error) {
p.iterationMu.Lock()
defer p.iterationMu.Unlock()
if p.currentIteration == nil {
return nil, ErrMissingIteration
}
return p.currentIteration.items(), nil
}
// CompletionRate returns stats on the completion rate of the puzzle.
func (p *Puzzle) CompletionRate() (*CompletionRate, error) {
p.iterationMu.Lock()
if p.currentIteration == nil {
p.iterationMu.Unlock()
return nil, ErrMissingIteration
}
c := p.currentIteration.completionRate()
p.iterationMu.Unlock()
c.Completed = c.FilledCells == c.TotalCells
p.attemptedIterationsMu.Lock()
c.AttemptedIterations = p.attemptedIterations
p.attemptedIterationsMu.Unlock()
p.errMu.Lock()
c.Error = p.err
p.errMu.Unlock()
p.timerMu.Lock()
c.CompletedAt = p.completedAt
c.FailedAt = p.failedAt
c.StartedAt = p.startedAt
p.timerMu.Unlock()
c.Failed = c.Error != nil
return c, nil
}
// CalculatePuzzleSize calculates the size of the puzzle.
// The puzzle size is the entire width of the puzzle.
func CalculatePuzzleSize(items []int) (int, error) {
puzzleSize := int(math.Sqrt(float64(len(items))))
return puzzleSize, nil
}
// CalculateSectionSize calculates the size each section in the puzzle.
func CalculateSectionSize(items []int, puzzleSize int) (int, error) {
if puzzleSize == 0 {
var err error
puzzleSize, err = CalculatePuzzleSize(items)
if err != nil {
return 0, err
}
}
sectionSize := int(math.Sqrt(float64(puzzleSize)))
return sectionSize, nil
}
// FormatPuzzle returns the given items as an [][]int
// so as you can easily print the results.
// Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,}
// Output: [][]int{
// []int{1, 2, 3, 4,},
// []int{5, 6, 7, 8,},
// []int{9, 10, 11, 12,},
// []int{13, 14, 15, 16,},
// }
func FormatPuzzle(items []int) ([][]int, error) {
puzzleSize, err := CalculatePuzzleSize(items)
if err != nil {
return nil, err
}
out := make([][]int, puzzleSize)
for y := 0; y < puzzleSize; y++ {
out[y] = make([]int, puzzleSize)
for x := 0; x < puzzleSize; x++ {
out[y][x] = items[(y*puzzleSize)+x]
}
}
return out, nil
}