-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.go
93 lines (76 loc) · 1.54 KB
/
day02.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
package day02
func check(e error) {
if e != nil {
panic(e)
}
}
func Part1(input string) int {
parser, err := getParser()
check(err)
games, err := parser.ParseString("", input)
check(err)
max := make(map[string]int, 0)
max["red"] = 12
max["blue"] = 14
max["green"] = 13
excludedGames := make([]int, 0)
for _, game := range games.Games {
for _, draw := range game.Draws {
for _, cube := range draw.Cubes {
color := cube.Color.cubeColorString()
if cube.Count > max[color] {
excludedGames = append(excludedGames, game.Id)
}
}
}
}
sum := 0
for _, game := range games.Games {
if !contains(excludedGames, game.Id) {
sum += game.Id
}
}
return sum
}
func Part2(input string) int {
parser, err := getParser()
check(err)
games, err := parser.ParseString("", input)
check(err)
powerSum := 0
for _, game := range games.Games {
max := make(map[string]int, 0)
max["red"] = 0
max["blue"] = 0
max["green"] = 0
for _, draw := range game.Draws {
for _, cube := range draw.Cubes {
color := cube.Color.cubeColorString()
if cube.Count > max[color] {
max[color] = cube.Count
}
}
}
powerSum += max["red"] * max["blue"] * max["green"]
}
return powerSum
}
func (c *Color) cubeColorString() string {
if c.Red {
return "red"
} else if c.Blue {
return "blue"
} else if c.Green {
return "green"
}
// never to be encountered unless we expand the grammar
return ""
}
func contains(ll []int, i int) bool {
for _, v := range ll {
if v == i {
return true
}
}
return false
}