generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
65 lines (49 loc) · 1.16 KB
/
solution.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
package day02p2
import (
"io"
"strconv"
"strings"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
sum := 0
for _, ln := range lines {
game := parseGame(ln)
maximums := make(map[string]int)
for _, draw := range game.Draws {
for k, v := range draw {
if v > maximums[k] {
maximums[k] = v
}
}
}
sum += maximums["red"] * maximums["green"] * maximums["blue"]
}
return sum
}
type Game struct {
Id int
Draws []map[string]int
}
func parseGame(line string) Game {
firstSplit := strings.Split(line, ": ")
idstrings := strings.Fields(firstSplit[0])
result := Game{}
var err error
result.Id, err = strconv.Atoi(idstrings[1])
utils.Check(err, "Unable to convert %s to integer", idstrings[1])
draws := strings.Split(firstSplit[1], "; ")
for _, draw := range draws {
drawmap := make(map[string]int)
colorsets := strings.Split(draw, ", ")
for _, colorset := range colorsets {
parts := strings.Fields(colorset)
n, err := strconv.Atoi(parts[0])
utils.Check(err, "Unable to convert %s to integer", parts[0])
drawmap[parts[1]] = n
}
result.Draws = append(result.Draws, drawmap)
}
return result
}