-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (105 loc) · 2.38 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
package main
//go:generate go run ./gen
import (
"fmt"
"io"
"os"
"runtime/pprof"
"strconv"
"time"
"aoc/utils"
"github.com/spf13/pflag"
)
var RunAll bool
var DaySelected int
var PartSelected string
var CpuProfile string
func init() {
pflag.BoolVarP(&RunAll, "all", "a", false, "run all days")
pflag.IntVarP(&DaySelected, "day", "d", 0, "run specific day")
pflag.StringVarP(&PartSelected, "part", "p", "2", "run specific part")
pflag.StringVar(&CpuProfile, "cpuprofile", "", "write cpu profile to `file`")
}
func main() {
pflag.Parse()
if CpuProfile != "" {
f, err := os.Create(CpuProfile)
utils.Check(err, "Unable to create cpu profile file")
defer f.Close()
err = pprof.StartCPUProfile(f)
utils.Check(err, "Unable to start CPU Profiler")
defer pprof.StopCPUProfile()
}
if RunAll {
runAll()
return
}
switch DaySelected {
case 0:
runCurrent()
default:
runDay(DaySelected, PartSelected)
}
}
type aocFunc func(io.Reader) any
type aocResult struct {
Result string
TimeElapsed time.Duration
}
type aocRunnerInput struct {
Name string
Func aocFunc
Filename string
Day int
Part string
}
func runAocPart(partFunc aocFunc, filename string) aocResult {
f, err := os.Open(filename)
utils.Check(err, "unable to open file %s", filename)
defer f.Close()
start := time.Now()
r := partFunc(f)
duration := time.Since(start)
res := aocResult{TimeElapsed: duration}
switch v := r.(type) {
case int:
res.Result = strconv.Itoa(v)
case int64:
res.Result = strconv.FormatInt(v, 10)
case uint64:
res.Result = strconv.FormatUint(v, 10)
case string:
res.Result = v
case fmt.Stringer:
res.Result = v.String()
default:
res.Result = "unknown return value"
}
return res
}
func runAll() {
var r aocResult
var total time.Duration
for _, v := range days {
r = runAocPart(v.Func, v.Filename)
total += r.TimeElapsed
fmt.Printf("%s: %s time elapsed: %s\n", v.Name, r.Result, r.TimeElapsed)
}
fmt.Printf("Overall time elapsed: %s\n", total)
}
func runDay(day int, part string) {
found := false
for _, v := range days {
if v.Day == day && v.Part == part {
fmt.Printf("Day %d part %s\n", day, part)
r := runAocPart(v.Func, v.Filename)
fmt.Println(r.Result)
fmt.Printf("Time elapsed: %s\n", r.TimeElapsed)
found = true
break
}
}
if !found {
fmt.Printf("Did not find a solution for day %d part %s\n", day, part)
}
}