/
main.go
135 lines (110 loc) · 2.78 KB
/
main.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
package main
import (
"flag"
"fmt"
"runtime"
"time"
"github.com/joshprzybyszewski/masyu/fetch"
"github.com/joshprzybyszewski/masyu/model"
"github.com/joshprzybyszewski/masyu/profile"
"github.com/joshprzybyszewski/masyu/results"
"github.com/joshprzybyszewski/masyu/solve"
)
var (
updateResults = flag.Bool("results", false, "if set, then it will print the custom benchmark results")
puzzID = flag.String("puzzID", "", "if set, then this will run a specific puzzle")
iterStart = flag.Int("start", int(model.MinIterator), "if set, this will override the iterators starting value")
iterFinish = flag.Int("finish", int(model.MaxIterator), "if set, this will override the iterators final value")
numIterations = flag.Int("numIterations", 1, "set this value to run through the puzzles many times")
fetchNewPuzzles = flag.Bool("refresh", true, "if set, then it will fetch new puzzles")
shouldProfile = flag.Bool("profile", false, "if set, will produce a profile output")
)
func main() {
flag.Parse()
if *updateResults {
results.Update()
return
}
if *shouldProfile {
defer profile.Start()()
}
if *puzzID != `` {
_ = runPuzzleID(
model.Iterator(*iterStart),
*puzzID,
)
return
}
for i := 0; i < *numIterations; i++ {
for iter := model.Iterator(*iterStart); iter <= model.Iterator(*iterFinish); iter++ {
for numGCs := 0; numGCs < 5; numGCs++ {
time.Sleep(100 * time.Millisecond)
runtime.GC()
}
err := compete(iter)
if err != nil {
fmt.Printf("Error: %+v\n", err)
// panic(err)
}
time.Sleep(time.Second)
}
}
}
func compete(iter model.Iterator) error {
fmt.Printf("Starting %s\n\t%s\n\n\n", iter, time.Now())
input, err := fetch.Puzzle(iter)
if *fetchNewPuzzles {
input, err = fetch.GetNewPuzzle(iter)
}
fmt.Printf("Iter: %q, PuzzleID: %q, Task: %q\n", iter, input.ID, input.Task())
if err != nil {
return err
}
ns := input.ToNodes()
t0 := time.Now()
sol, err := solve.FromNodes(
iter.GetSize(),
ns,
)
defer func(dur time.Duration) {
fmt.Printf("Input: %s\n", input)
fmt.Printf("Solution:\n%s\n", sol.Pretty(ns))
fmt.Printf("Duration: %s\n\n\n", dur)
}(time.Since(t0))
if err != nil {
_ = fetch.StorePuzzle(&input)
return err
}
return fetch.Submit(
&input,
&sol,
)
}
func runPuzzleID(
iter model.Iterator,
id string,
) error {
fmt.Printf("Starting %s\n\t%s\n\n\n", iter, time.Now())
input, err := fetch.GetPuzzleID(iter, id)
if err != nil {
return err
}
ns := input.ToNodes()
t0 := time.Now()
sol, err := solve.FromNodes(
iter.GetSize(),
ns,
)
defer func(dur time.Duration) {
fmt.Printf("Input: %s\n", input)
fmt.Printf("Solution:\n%s\n", sol.Pretty(ns))
fmt.Printf("Duration: %s\n\n\n", dur)
}(time.Since(t0))
if err != nil {
return err
}
return fetch.Submit(
&input,
&sol,
)
}