-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.go
93 lines (76 loc) · 1.86 KB
/
solver.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
package main
import (
"fmt"
"os"
"os/signal"
"github.com/arcenik/go-curiositybox-calendar-puzzle-solver/board"
"github.com/arcenik/go-curiositybox-calendar-puzzle-solver/dbsqlite3"
)
type SolveAllCmd struct {
}
func (c *SolveAllCmd) Run(globals *Globals) error {
var (
b board.Board = board.NewBoard()
s board.Solution
err error
debug bool = globals.Debug
)
db := dbsqlite3.InitDB("results.db") // FIXME moves to parameters and set optional
defer db.Close()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
go func() {
for range sigChan {
fmt.Println("Ctrl-C catched, closing DB")
db.Close()
os.Exit(1)
}
}()
s, err = board.NewSolution()
if err != nil {
fmt.Println("New returned an error:", err)
return err
}
fmt.Println("Starting to solve all solutions")
b.SolutionBootstrap(&s)
for b.SolutionWalker(&s) {
if b.Completed {
if debug {
fmt.Println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
b.PrintSolution(s)
b.PrintBoard()
// fmt.Println("completed", b.Completed)
// fmt.Println("check and save")
}
month, day, err := b.GetDate()
if err != nil {
if debug {
fmt.Println("the solution is not valid")
}
} else {
if debug {
fmt.Printf("Solution for %v %v\n", month, day)
}
json, err := s.ToJSON()
if err != nil {
fmt.Println("Error extract JSON from solution:", err)
} else {
_ = db.InsertSolution(month, day, json)
// TODO: ignore just the error for unique constraint on solutions.json
// UNIQUE constraint failed: solutions.json
}
}
// remove the last piece and continue
s[board.SolutionMax].OnBoard = false
b.LoadSolution(s)
b.Completed = false
if debug {
fmt.Print(">>")
fmt.Scanln()
}
b.Completed = false
}
}
fmt.Println("Completed")
return nil
}