-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (68 loc) · 1.25 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
package main
import (
"fmt"
"strings"
"github.com/devkvlt/aoc"
)
type subset = map[string]int
func parseGame(line string) []subset {
g := []subset{}
for _, sub := range strings.Split(strings.Split(line, ": ")[1], "; ") {
colors := strings.Split(sub, ", ")
m := map[string]int{}
g = append(g, m)
for _, color := range colors {
c := ""
n := 0
fmt.Sscanf(color, "%d %s", &n, &c)
m[c] = n
}
}
return g
}
func isPossible(g []subset) bool {
for _, sub := range g {
if sub["red"] > 12 || sub["green"] > 13 || sub["blue"] > 14 {
return false
}
}
return true
}
func power(g []subset) int {
maxRed := 0
maxGreen := 0
maxBlue := 0
for _, sub := range g {
if sub["red"] > maxRed {
maxRed = sub["red"]
}
if sub["green"] > maxGreen {
maxGreen = sub["green"]
}
if sub["blue"] > maxBlue {
maxBlue = sub["blue"]
}
}
return maxRed * maxGreen * maxBlue
}
func main() {
lines := aoc.Lines("input")
games := make([][]subset, len(lines))
for i, line := range lines {
games[i] = parseGame(line)
}
// Part 1
result := 0
for i, g := range games {
if isPossible(g) {
result += i + 1
}
}
fmt.Println(result)
// Part 2
result2 := 0
for _, g := range games {
result2 += power(g)
}
fmt.Println(result2)
}