-
Notifications
You must be signed in to change notification settings - Fork 1
/
14.go
95 lines (83 loc) · 1.6 KB
/
14.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
)
type reindeer struct {
speed int
duration int
rest int
distance int
points int
}
func format(line string) reindeer {
re := regexp.MustCompile("fly ([0-9]+) km/s for ([0-9]+) seconds, but then must rest for ([0-9]+) seconds")
res := re.FindStringSubmatch(line)
if res == nil {
log.Fatal("Regex failed on input", line)
}
speed, err := strconv.Atoi(res[1])
if err != nil {
log.Fatal(err)
}
duration, err := strconv.Atoi(res[2])
if err != nil {
log.Fatal(err)
}
rest, err := strconv.Atoi(res[3])
if err != nil {
log.Fatal(err)
}
return reindeer{speed: speed, duration: duration, rest: rest, distance: 0, points: 0}
}
func main() {
file, err := os.Open("input/14.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
var reindeers []reindeer
scanner := bufio.NewScanner(file)
for scanner.Scan() {
reindeers = append(reindeers, format(scanner.Text()))
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
max := 0
for i := 0; i < 2503; i++ {
// update distance
for j, r := range reindeers {
t := i % (r.duration + r.rest)
if t < r.duration {
r.distance += r.speed
reindeers[j] = r
if r.distance > max {
max = r.distance
}
}
}
// update points
for j, r := range reindeers {
if r.distance == max {
r.points++
reindeers[j] = r
}
}
}
distance, points := 0, 0
for _, r := range reindeers {
if r.distance > distance {
distance = r.distance
}
if r.points > points {
points = r.points
}
}
fmt.Println(distance)
fmt.Println(points)
}