0
|
1 |
package takuzu |
|
2 |
|
|
3 |
import ( |
|
4 |
"bytes" |
|
5 |
"fmt" |
|
6 |
"log" |
|
7 |
"math" |
|
8 |
"runtime" |
|
9 |
"sync" |
|
10 |
"time" |
|
11 |
|
|
12 |
"github.com/pkg/errors" |
|
13 |
) |
|
14 |
|
|
15 |
var verbosity int |
|
16 |
var schrodLvl uint |
|
17 |
|
|
18 |
// Cell is a single cell of a Takuzu game board |
|
19 |
type Cell struct { |
|
20 |
Defined bool |
|
21 |
Value int |
|
22 |
} |
|
23 |
|
|
24 |
// Takuzu is a Takuzu game board (Size x Size) |
|
25 |
type Takuzu struct { |
|
26 |
Size int |
|
27 |
Board [][]Cell |
|
28 |
} |
|
29 |
|
|
30 |
// New creates a new Takuzu board |
|
31 |
func New(size int) Takuzu { |
|
32 |
t := Takuzu{Size: size} |
|
33 |
t.Board = make([][]Cell, size) |
|
34 |
for l := range t.Board { |
|
35 |
t.Board[l] = make([]Cell, size) |
|
36 |
} |
|
37 |
return t |
|
38 |
} |
|
39 |
|
|
40 |
// NewFromString creates a new Takuzu board from a string definition |
|
41 |
func NewFromString(s string) (*Takuzu, error) { |
|
42 |
l := len(s) |
2
|
43 |
if l < 4 { |
|
44 |
return nil, errors.New("bad string length") |
|
45 |
} |
|
46 |
|
0
|
47 |
size := int(math.Sqrt(float64(l))) |
|
48 |
if size*size != l { |
|
49 |
return nil, errors.New("bad string length") |
|
50 |
} |
|
51 |
|
2
|
52 |
// TODO: validate chars ([.01OI]) |
|
53 |
|
0
|
54 |
i := 0 |
|
55 |
t := New(size) |
|
56 |
|
|
57 |
for line := 0; line < size; line++ { |
|
58 |
for col := 0; col < size; col++ { |
|
59 |
switch s[i] { |
|
60 |
case '0', 'O': |
|
61 |
t.Board[line][col].Defined = true |
|
62 |
t.Board[line][col].Value = 0 |
|
63 |
case '1', 'I': |
|
64 |
t.Board[line][col].Defined = true |
|
65 |
t.Board[line][col].Value = 1 |
|
66 |
case '.': |
|
67 |
default: |
|
68 |
return nil, errors.New("invalid char in string") |
|
69 |
} |
|
70 |
i++ |
|
71 |
} |
|
72 |
} |
|
73 |
return &t, nil |
|
74 |
} |
|
75 |
|
|
76 |
// ToString converts a takuzu board to its string representation |
|
77 |
func (b Takuzu) ToString() string { |
|
78 |
var sbuf bytes.Buffer |
|
79 |
for line := 0; line < b.Size; line++ { |
|
80 |
for col := 0; col < b.Size; col++ { |
|
81 |
if b.Board[line][col].Defined { |
|
82 |
sbuf.WriteString(fmt.Sprintf("%d", b.Board[line][col].Value)) |
|
83 |
continue |
|
84 |
} |
|
85 |
sbuf.WriteByte('.') |
|
86 |
} |
|
87 |
} |
|
88 |
return sbuf.String() |
|
89 |
} |
|
90 |
|
|
91 |
// DumpString writes the content of the board as a stream |
|
92 |
func (b Takuzu) DumpString() { |
|
93 |
fmt.Println(b.ToString()) |
|
94 |
} |
|
95 |
|
|
96 |
// Clone returns a copy of the Takuzu board |
|
97 |
func (b Takuzu) Clone() Takuzu { |
|
98 |
c := New(b.Size) |
|
99 |
for line := range b.Board { |
|
100 |
for col := range b.Board[line] { |
|
101 |
c.Board[line][col] = b.Board[line][col] |
|
102 |
} |
|
103 |
} |
|
104 |
return c |
|
105 |
} |
|
106 |
|
|
107 |
// Copy copies a Takuzu board to another existing board |
|
108 |
func Copy(src, dst *Takuzu) error { |
|
109 |
if src.Size != dst.Size { |
|
110 |
return errors.New("sizes do not match") |
|
111 |
} |
|
112 |
for line := range src.Board { |
|
113 |
for col := range src.Board[line] { |
|
114 |
dst.Board[line][col] = src.Board[line][col] |
|
115 |
} |
|
116 |
} |
|
117 |
return nil |
|
118 |
} |
|
119 |
|
|
120 |
// BoardsMatch compares a Takuzu board to another, optionally ignoring |
|
121 |
// empty cells. Returns true if the two boards match. |
|
122 |
func BoardsMatch(t1, t2 *Takuzu, ignoreUndefined bool) (match bool, line, col int) { |
|
123 |
match = true |
|
124 |
|
2
|
125 |
if t1 == nil || t2 == nil { |
|
126 |
line, col = -1, -1 |
|
127 |
match = false |
|
128 |
return |
|
129 |
} |
|
130 |
|
0
|
131 |
if t1.Size != t2.Size { |
|
132 |
line, col = -1, -1 |
|
133 |
match = false |
|
134 |
return |
|
135 |
} |
2
|
136 |
|
0
|
137 |
for line = range t1.Board { |
|
138 |
for col = range t1.Board[line] { |
|
139 |
if !t1.Board[line][col].Defined || !t2.Board[line][col].Defined { |
|
140 |
// At least one of the cells is empty |
|
141 |
if ignoreUndefined || |
|
142 |
!(t1.Board[line][col].Defined || t2.Board[line][col].Defined) { |
|
143 |
// Both cells are empty or we ignore empty cells |
|
144 |
continue |
|
145 |
} |
|
146 |
match = false |
|
147 |
return |
|
148 |
} |
|
149 |
// Both cells are defined |
|
150 |
if t1.Board[line][col].Value != t2.Board[line][col].Value { |
|
151 |
match = false |
|
152 |
return |
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
|
|
157 |
line, col = -1, -1 |
|
158 |
return |
|
159 |
} |
|
160 |
|
|
161 |
// Set sets the value of the cell; a value -1 will set the cell as undefined |
|
162 |
func (c *Cell) Set(value int) { |
|
163 |
if value != 0 && value != 1 { |
|
164 |
c.Defined = false |
|
165 |
return |
|
166 |
} |
|
167 |
c.Defined = true |
|
168 |
c.Value = value |
|
169 |
} |
|
170 |
|
|
171 |
// Set sets the value of a specific cell |
|
172 |
// A value -1 will undefine the cell |
|
173 |
func (b Takuzu) Set(l, c, value int) { |
|
174 |
if value != 0 && value != 1 { |
|
175 |
b.Board[l][c].Defined = false |
|
176 |
return |
|
177 |
} |
|
178 |
b.Board[l][c].Defined = true |
|
179 |
b.Board[l][c].Value = value |
|
180 |
} |
|
181 |
|
|
182 |
// GetLine returns a slice of cells containing the ith line of the board |
|
183 |
func (b Takuzu) GetLine(i int) []Cell { |
|
184 |
return b.Board[i] |
|
185 |
} |
|
186 |
|
|
187 |
// GetColumn returns a slice of cells containing the ith column of the board |
|
188 |
func (b Takuzu) GetColumn(i int) []Cell { |
|
189 |
c := make([]Cell, b.Size) |
|
190 |
for l := range b.Board { |
|
191 |
c[l] = b.Board[l][i] |
|
192 |
} |
|
193 |
return c |
|
194 |
} |
|
195 |
|
|
196 |
// GetLinePointers returns a slice of pointers to the cells of the ith line of the board |
|
197 |
func (b Takuzu) GetLinePointers(i int) []*Cell { |
|
198 |
r := make([]*Cell, b.Size) |
|
199 |
for l := range b.Board[i] { |
|
200 |
r[l] = &b.Board[i][l] |
|
201 |
} |
|
202 |
return r |
|
203 |
} |
|
204 |
|
|
205 |
// GetColumnPointers returns a slice of pointers to the cells of the ith column of the board |
|
206 |
func (b Takuzu) GetColumnPointers(i int) []*Cell { |
|
207 |
r := make([]*Cell, b.Size) |
|
208 |
for l := range b.Board { |
|
209 |
r[l] = &b.Board[l][i] |
|
210 |
} |
|
211 |
return r |
|
212 |
} |
|
213 |
|
|
214 |
// FillLineColumn add missing 0s or 1s if all 1s or 0s are there. |
|
215 |
// Note: This method can update b. |
|
216 |
func (b Takuzu) FillLineColumn(l, c int) { |
|
217 |
fillRange := func(r []*Cell) { |
|
218 |
size := len(r) |
|
219 |
var notFull bool |
|
220 |
var n [2]int |
|
221 |
for x := 0; x < size; x++ { |
|
222 |
if r[x].Defined { |
|
223 |
n[r[x].Value]++ |
|
224 |
} else { |
|
225 |
notFull = true |
|
226 |
} |
|
227 |
} |
|
228 |
if !notFull { |
|
229 |
return |
|
230 |
} |
|
231 |
if n[0] == size/2 { |
|
232 |
// Let's fill the 1s |
|
233 |
for _, x := range r { |
|
234 |
if !x.Defined { |
|
235 |
x.Defined = true |
|
236 |
x.Value = 1 |
|
237 |
} |
|
238 |
} |
|
239 |
} else if n[1] == size/2 { |
|
240 |
// Let's fill the 0s |
|
241 |
for _, x := range r { |
|
242 |
if !x.Defined { |
|
243 |
x.Defined = true |
|
244 |
x.Value = 0 |
|
245 |
} |
|
246 |
} |
|
247 |
} |
|
248 |
} |
|
249 |
|
|
250 |
var cells []*Cell |
|
251 |
|
|
252 |
// Fill line |
|
253 |
cells = b.GetLinePointers(l) |
|
254 |
fillRange(cells) |
|
255 |
// Fill column |
|
256 |
cells = b.GetColumnPointers(c) |
|
257 |
fillRange(cells) |
|
258 |
} |
|
259 |
|
|
260 |
// DumpBoard displays the Takuzu board |
|
261 |
func (b Takuzu) DumpBoard() { |
|
262 |
fmt.Println() |
|
263 |
for i := range b.Board { |
|
264 |
dumpRange(b.Board[i]) |
|
265 |
} |
|
266 |
} |
|
267 |
|
|
268 |
// CheckLine returns an error if the line i fails validation |
|
269 |
func (b Takuzu) CheckLine(i int) error { |
|
270 |
_, err := checkRange(b.GetLine(i)) |
|
271 |
return err |
|
272 |
} |
|
273 |
|
|
274 |
// CheckColumn returns an error if the column i fails validation |
|
275 |
func (b Takuzu) CheckColumn(i int) error { |
|
276 |
_, err := checkRange(b.GetColumn(i)) |
|
277 |
return err |
|
278 |
} |
|
279 |
|
|
280 |
// Validate checks a whole board for errors (not completeness) |
|
281 |
// Returns true if all cells are defined. |
|
282 |
func (b Takuzu) Validate() (bool, error) { |
|
283 |
finished := true |
|
284 |
|
|
285 |
computeVal := func(cells []Cell) (val int) { |
|
286 |
for i := 0; i < len(cells); i++ { |
|
287 |
val += cells[i].Value * 1 << uint(i) |
|
288 |
} |
|
289 |
return |
|
290 |
} |
|
291 |
|
|
292 |
lineVals := make(map[int]bool) |
|
293 |
colVals := make(map[int]bool) |
|
294 |
|
|
295 |
for i := 0; i < b.Size; i++ { |
|
296 |
var d []Cell |
|
297 |
var full bool |
|
298 |
var err error |
|
299 |
|
|
300 |
// Let's check line i |
|
301 |
d = b.GetLine(i) |
|
302 |
full, err = checkRange(d) |
|
303 |
if err != nil { |
|
304 |
return false, errors.Wrapf(err, "line %d", i) |
|
305 |
} |
|
306 |
if full { |
|
307 |
hv := computeVal(d) |
|
308 |
if lineVals[hv] { |
|
309 |
return false, fmt.Errorf("duplicate lines (%d)", i) |
|
310 |
} |
|
311 |
lineVals[hv] = true |
|
312 |
} else { |
|
313 |
finished = false |
|
314 |
} |
|
315 |
|
|
316 |
// Let's check column i |
|
317 |
d = b.GetColumn(i) |
|
318 |
full, err = checkRange(d) |
|
319 |
if err != nil { |
|
320 |
return false, errors.Wrapf(err, "column %d", i) |
|
321 |
} |
|
322 |
if full { |
|
323 |
hv := computeVal(d) |
|
324 |
if colVals[hv] { |
3
|
325 |
return false, fmt.Errorf("duplicate columns (%d)", i) |
0
|
326 |
} |
|
327 |
colVals[hv] = true |
|
328 |
} else { |
|
329 |
finished = false |
|
330 |
} |
|
331 |
} |
|
332 |
return finished, nil |
|
333 |
} |
|
334 |
|
|
335 |
func dumpRange(cells []Cell) { |
|
336 |
for _, c := range cells { |
|
337 |
if !c.Defined { |
|
338 |
fmt.Printf(". ") |
|
339 |
continue |
|
340 |
} |
|
341 |
fmt.Printf("%d ", c.Value) |
|
342 |
} |
|
343 |
fmt.Println() |
|
344 |
} |
|
345 |
|
|
346 |
// checkRange returns true if the range is completely defined, and an error |
|
347 |
// if it doesn't follow the rules for a takuzu line or column |
|
348 |
// Note that the boolean might be invalid if the error is not nil. |
|
349 |
func checkRange(cells []Cell) (bool, error) { |
|
350 |
full := true |
|
351 |
size := len(cells) |
|
352 |
counters := []int{0, 0} |
|
353 |
|
|
354 |
var prevCell Cell |
|
355 |
var prevCellCount int |
|
356 |
|
|
357 |
for _, c := range cells { |
|
358 |
if !c.Defined { |
|
359 |
full = false |
|
360 |
prevCell.Defined = false |
|
361 |
prevCellCount = 0 |
|
362 |
continue |
|
363 |
} |
|
364 |
counters[c.Value]++ |
|
365 |
if prevCellCount == 0 { |
|
366 |
prevCellCount = 1 |
|
367 |
} else { |
|
368 |
if c.Value == prevCell.Value { |
|
369 |
prevCellCount++ |
|
370 |
if prevCellCount > 2 { |
|
371 |
return full, errors.Errorf("3+ same values %d", c.Value) |
|
372 |
} |
|
373 |
} else { |
|
374 |
prevCellCount = 1 |
|
375 |
} |
|
376 |
|
|
377 |
} |
|
378 |
prevCell = c |
|
379 |
} |
|
380 |
if counters[0] > size/2 { |
|
381 |
return full, errors.Errorf("too many zeroes") |
|
382 |
} |
|
383 |
if counters[1] > size/2 { |
|
384 |
return full, errors.Errorf("too many ones") |
|
385 |
} |
|
386 |
return full, nil |
|
387 |
} |
|
388 |
|
|
389 |
func checkRangeCounts(cells []Cell) (full bool, n0, n1 int) { |
|
390 |
counters := []int{0, 0} |
|
391 |
full = true |
|
392 |
|
|
393 |
for _, c := range cells { |
|
394 |
if c.Defined { |
|
395 |
counters[c.Value]++ |
|
396 |
} else { |
|
397 |
full = false |
|
398 |
} |
|
399 |
} |
|
400 |
return full, counters[0], counters[1] |
|
401 |
} |
|
402 |
|
|
403 |
func (b Takuzu) guessPos(l, c int) int { |
|
404 |
if b.Board[l][c].Defined { |
|
405 |
return b.Board[l][c].Value |
|
406 |
} |
|
407 |
|
|
408 |
bx := b.Clone() |
|
409 |
bx.Set(l, c, 0) |
|
410 |
bx.FillLineColumn(l, c) |
|
411 |
if bx.CheckLine(l) != nil || bx.CheckColumn(c) != nil { |
|
412 |
return 1 |
|
413 |
} |
|
414 |
Copy(&b, &bx) |
|
415 |
bx.Set(l, c, 1) |
|
416 |
bx.FillLineColumn(l, c) |
|
417 |
if bx.CheckLine(l) != nil || bx.CheckColumn(c) != nil { |
|
418 |
return 0 |
|
419 |
} |
|
420 |
|
|
421 |
return -1 // dunno |
|
422 |
} |
|
423 |
|
|
424 |
// trySolveTrivialPass does 1 pass over the takuzu board and tries to find |
|
425 |
// values using simple guesses. |
|
426 |
func (b Takuzu) trySolveTrivialPass() (changed bool) { |
|
427 |
for line := 0; line < b.Size; line++ { |
|
428 |
for col := 0; col < b.Size; col++ { |
|
429 |
if b.Board[line][col].Defined { |
|
430 |
continue |
|
431 |
} |
|
432 |
if guess := b.guessPos(line, col); guess != -1 { |
|
433 |
b.Set(line, col, guess) |
|
434 |
if verbosity > 3 { |
|
435 |
log.Printf("Trivial: Setting [%d,%d] to %d", line, col, guess) |
|
436 |
} |
|
437 |
changed = true // Ideally remember l,c |
|
438 |
} |
|
439 |
} |
|
440 |
} |
|
441 |
return changed |
|
442 |
} |
|
443 |
|
|
444 |
// TrySolveTrivial tries to solve the takuzu using a loop over simple methods |
|
445 |
// It returns true if all cells are defined, and an error if the grid breaks the rules. |
|
446 |
func (b Takuzu) TrySolveTrivial() (bool, error) { |
|
447 |
for { |
|
448 |
changed := b.trySolveTrivialPass() |
|
449 |
if verbosity > 3 { |
|
450 |
var status string |
|
451 |
if changed { |
|
452 |
status = "ongoing" |
|
453 |
} else { |
|
454 |
status = "stuck" |
|
455 |
} |
|
456 |
log.Println("Trivial resolution -", status) |
|
457 |
} |
|
458 |
if !changed { |
|
459 |
break |
|
460 |
} |
|
461 |
|
|
462 |
if verbosity > 3 { |
|
463 |
b.DumpBoard() |
|
464 |
fmt.Println() |
|
465 |
} |
|
466 |
} |
|
467 |
full, err := b.Validate() |
|
468 |
if err != nil { |
|
469 |
return full, errors.Wrap(err, "the takuzu looks wrong") |
|
470 |
} |
|
471 |
return full, nil |
|
472 |
} |
|
473 |
|
|
474 |
// TrySolveRecurse tries to solve the takuzu recursively, using trivial |
|
475 |
// method first and using guesses if it fails. |
|
476 |
func (b Takuzu) TrySolveRecurse(allSolutions *[]Takuzu, timeout time.Duration) (*Takuzu, error) { |
|
477 |
|
|
478 |
var solutionsMux sync.Mutex |
|
479 |
var singleSolution *Takuzu |
|
480 |
var solutionMap map[string]*Takuzu |
|
481 |
|
|
482 |
var globalSearch bool |
|
483 |
// globalSearch doesn't need to use a mutex and is more convenient |
|
484 |
// to use than allSolutions. |
|
485 |
if allSolutions != nil { |
|
486 |
globalSearch = true |
|
487 |
solutionMap = make(map[string]*Takuzu) |
|
488 |
} |
|
489 |
|
|
490 |
startTime := time.Now() |
|
491 |
|
|
492 |
var recurseSolve func(level int, t Takuzu, errStatus chan<- error) error |
|
493 |
|
|
494 |
recurseSolve = func(level int, t Takuzu, errStatus chan<- error) error { |
|
495 |
|
|
496 |
reportStatus := func(failure error) { |
|
497 |
// Report status to the caller's channel |
|
498 |
if errStatus != nil { |
|
499 |
errStatus <- failure |
|
500 |
} |
|
501 |
} |
|
502 |
|
|
503 |
// In Schröndinger mode we check concurrently both values for a cell |
|
504 |
var schrodinger bool |
|
505 |
concurrentRoutines := 1 |
|
506 |
if level < int(schrodLvl) { |
|
507 |
schrodinger = true |
|
508 |
concurrentRoutines = 2 |
|
509 |
} |
|
510 |
|
|
511 |
var status [2]chan error |
|
512 |
status[0] = make(chan error) |
|
513 |
status[1] = make(chan error) |
|
514 |
|
|
515 |
for { |
|
516 |
// Try simple resolution first |
|
517 |
full, err := t.TrySolveTrivial() |
|
518 |
if err != nil { |
|
519 |
reportStatus(err) |
|
520 |
return err |
|
521 |
} |
|
522 |
|
|
523 |
if full { // We're done |
|
524 |
if verbosity > 1 { |
|
525 |
log.Printf("{%d} The takuzu is correct and complete.", level) |
|
526 |
} |
|
527 |
solutionsMux.Lock() |
|
528 |
singleSolution = &t |
|
529 |
if globalSearch { |
|
530 |
solutionMap[t.ToString()] = &t |
|
531 |
} |
|
532 |
solutionsMux.Unlock() |
|
533 |
|
|
534 |
reportStatus(nil) |
|
535 |
return nil |
|
536 |
} |
|
537 |
|
|
538 |
if verbosity > 2 { |
|
539 |
log.Printf("{%d} Trivial resolution did not complete.", level) |
|
540 |
} |
|
541 |
|
|
542 |
// Trivial method is stuck, let's use recursion |
|
543 |
|
|
544 |
changed := false |
|
545 |
|
|
546 |
// Looking for first empty cell |
|
547 |
var line, col int |
|
548 |
firstClear: |
|
549 |
for line = 0; line < t.Size; line++ { |
|
550 |
for col = 0; col < t.Size; col++ { |
|
551 |
if !t.Board[line][col].Defined { |
|
552 |
break firstClear |
|
553 |
} |
|
554 |
} |
|
555 |
} |
|
556 |
|
|
557 |
if line == t.Size || col == t.Size { |
|
558 |
break |
|
559 |
} |
|
560 |
|
|
561 |
if verbosity > 2 { |
|
562 |
log.Printf("{%d} GUESS - Trying values for [%d,%d]", level, line, col) |
|
563 |
} |
|
564 |
|
|
565 |
var val int |
|
566 |
err = nil |
|
567 |
errCount := 0 |
|
568 |
|
|
569 |
for testval := 0; testval < 2; testval++ { |
|
570 |
if !globalSearch && t.Board[line][col].Defined { |
|
571 |
// No need to "guess" here anymore |
|
572 |
break |
|
573 |
} |
|
574 |
|
|
575 |
// Launch goroutines for cell values of 0 and/or 1 |
|
576 |
for testCase := 0; testCase < 2; testCase++ { |
|
577 |
if schrodinger || testval == testCase { |
|
578 |
tx := t.Clone() |
|
579 |
tx.Set(line, col, testCase) |
|
580 |
go recurseSolve(level+1, tx, status[testCase]) |
|
581 |
} |
|
582 |
} |
|
583 |
|
|
584 |
// Let's collect the goroutines' results |
|
585 |
for i := 0; i < concurrentRoutines; i++ { |
|
586 |
if schrodinger && verbosity > 1 { // XXX |
|
587 |
log.Printf("{%d} Schrodinger waiting for result #%d for cell [%d,%d]", level, i, line, col) |
|
588 |
} |
|
589 |
select { |
|
590 |
case e := <-status[0]: |
|
591 |
err = e |
|
592 |
val = 0 |
|
593 |
case e := <-status[1]: |
|
594 |
err = e |
|
595 |
val = 1 |
|
596 |
} |
|
597 |
|
|
598 |
if schrodinger && verbosity > 1 { // XXX |
|
599 |
log.Printf("{%d} Schrodinger result #%d/2 for cell [%d,%d]=%d - err=%v", level, i+1, line, col, val, err) |
|
600 |
} |
|
601 |
|
|
602 |
if err == nil { |
|
603 |
if !globalSearch { |
|
604 |
reportStatus(nil) |
|
605 |
if i+1 < concurrentRoutines { |
|
606 |
// Schröndinger mode and we still have one status to fetch |
|
607 |
<-status[1-val] |
|
608 |
} |
|
609 |
return nil |
|
610 |
} |
|
611 |
continue |
|
612 |
} |
|
613 |
if timeout > 0 && level > 2 && time.Since(startTime) > timeout { |
|
614 |
if errors.Cause(err).Error() != "timeout" { |
|
615 |
if verbosity > 0 { |
|
616 |
log.Printf("{%d} Timeout, giving up", level) |
|
617 |
} |
|
618 |
err := errors.New("timeout") |
|
619 |
reportStatus(err) |
|
620 |
if i+1 < concurrentRoutines { |
|
621 |
// Schröndinger mode and we still have one status to fetch |
|
622 |
<-status[1-val] |
|
623 |
} |
|
624 |
// XXX actually can't close the channel and leave, can I? |
|
625 |
return err |
|
626 |
} |
|
627 |
} |
|
628 |
|
|
629 |
// err != nil: we can set a value -- unless this was a timeout |
|
630 |
if errors.Cause(err).Error() == "timeout" { |
|
631 |
if verbosity > 1 { |
|
632 |
log.Printf("{%d} Timeout propagation", level) |
|
633 |
} |
|
634 |
reportStatus(err) |
|
635 |
if i+1 < concurrentRoutines { |
|
636 |
// Schröndinger mode and we still have one status to fetch |
|
637 |
<-status[1-val] |
|
638 |
} |
|
639 |
// XXX actually can't close the channel and leave, can I? |
|
640 |
return err |
|
641 |
} |
|
642 |
|
|
643 |
errCount++ |
|
644 |
if verbosity > 2 { |
|
645 |
log.Printf("{%d} Bad outcome (%v)", level, err) |
|
646 |
log.Printf("{%d} GUESS was wrong - Setting [%d,%d] to %d", |
|
647 |
level, line, col, 1-val) |
|
648 |
} |
|
649 |
|
|
650 |
t.Set(line, col, 1-val) |
|
651 |
changed = true |
|
652 |
|
|
653 |
} // concurrentRoutines |
|
654 |
|
|
655 |
if (changed && !globalSearch) || schrodinger { |
|
656 |
// Let's loop again with the new board |
|
657 |
break |
|
658 |
} |
|
659 |
} |
|
660 |
|
|
661 |
if verbosity > 2 { |
|
662 |
log.Printf("{%d} End of cycle.\n\n", level) |
|
663 |
} |
|
664 |
|
|
665 |
if errCount == 2 { |
|
666 |
// Both values failed |
|
667 |
err := errors.New("dead end") |
|
668 |
reportStatus(err) |
|
669 |
return err |
|
670 |
} |
|
671 |
|
|
672 |
// If we cannot fill more cells (!changed) or if we've made a global search with |
|
673 |
// both values, the search is complete. |
|
674 |
if schrodinger || globalSearch || !changed { |
|
675 |
break |
|
676 |
} |
|
677 |
|
|
678 |
if verbosity > 2 { |
|
679 |
t.DumpBoard() |
|
680 |
fmt.Println() |
|
681 |
} |
|
682 |
} |
|
683 |
|
|
684 |
// Try to force garbage collection |
|
685 |
runtime.GC() |
|
686 |
|
|
687 |
full, err := t.Validate() |
|
688 |
if err != nil { |
|
689 |
if verbosity > 1 { |
|
690 |
log.Println("The takuzu looks wrong - ", err) |
|
691 |
} |
|
692 |
err := errors.Wrap(err, "the takuzu looks wrong") |
|
693 |
reportStatus(err) |
|
694 |
return err |
|
695 |
} |
|
696 |
if full { |
|
697 |
if verbosity > 1 { |
|
698 |
log.Println("The takuzu is correct and complete") |
|
699 |
} |
|
700 |
solutionsMux.Lock() |
|
701 |
singleSolution = &t |
|
702 |
if globalSearch { |
|
703 |
solutionMap[t.ToString()] = &t |
|
704 |
} |
|
705 |
solutionsMux.Unlock() |
|
706 |
} |
|
707 |
|
|
708 |
reportStatus(nil) |
|
709 |
return nil |
|
710 |
} |
|
711 |
|
|
712 |
status := make(chan error) |
|
713 |
go recurseSolve(0, b, status) |
|
714 |
|
|
715 |
err := <-status // Wait for it... |
|
716 |
|
|
717 |
firstSol := singleSolution |
|
718 |
if globalSearch { |
|
719 |
for _, tp := range solutionMap { |
|
720 |
*allSolutions = append(*allSolutions, *tp) |
|
721 |
} |
|
722 |
} |
|
723 |
|
|
724 |
if err != nil { |
|
725 |
return firstSol, err |
|
726 |
} |
|
727 |
|
|
728 |
if globalSearch && len(*allSolutions) > 0 { |
|
729 |
firstSol = &(*allSolutions)[0] |
|
730 |
} |
|
731 |
return firstSol, nil |
|
732 |
} |
|
733 |
|
|
734 |
// SetSchrodingerLevel initializes the "Schrödinger" level (0 means disabled) |
|
735 |
// It must be called before any board generation or reduction. |
|
736 |
func SetSchrodingerLevel(level uint) { |
|
737 |
schrodLvl = level |
|
738 |
} |
|
739 |
|
|
740 |
// SetVerbosityLevel initializes the verbosity level |
|
741 |
func SetVerbosityLevel(level int) { |
|
742 |
verbosity = level |
|
743 |
} |