-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (63 loc) · 1.6 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
package main
import (
"regexp"
"github.com/danvolchek/AdventOfCode/lib"
)
type reindeer struct {
name string
speed, duration, restPeriod int
}
var parseRegexp = regexp.MustCompile(`(.+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds.`)
func parse(parts []string) reindeer {
return reindeer{
name: parts[0],
speed: lib.Atoi(parts[1]),
duration: lib.Atoi(parts[2]),
restPeriod: lib.Atoi(parts[3]),
}
}
func (r reindeer) distance(time int) int {
distance := 0
flying := true
flyTimeLeft := r.duration
restTimeLeft := 0
for time > 0 {
if flying {
flyTime := lib.Min(time, flyTimeLeft)
distance += r.speed * flyTime
flyTimeLeft -= flyTime
time -= flyTime
if flyTimeLeft == 0 {
restTimeLeft = r.restPeriod
flying = false
}
} else {
restTime := lib.Min(time, restTimeLeft)
restTimeLeft -= restTime
time -= restTime
if restTimeLeft == 0 {
flyTimeLeft = r.duration
flying = true
}
}
}
return distance
}
var totalTime int
func solve(reindeers []reindeer) int {
distances := lib.Map(reindeers, func(reindeer reindeer) int {
return reindeer.distance(totalTime)
})
return lib.Max(distances...)
}
func main() {
solver := lib.Solver[[]reindeer, int]{
ParseF: lib.ParseLine(lib.ParseRegexp(parseRegexp, parse)),
SolveF: solve,
}
totalTime = 1000
solver.Expect("Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.", 1120)
solver.Expect("Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.", 1056)
totalTime = 2503
solver.Verify(2640)
}