-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
98 lines (86 loc) · 1.71 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
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"bufio"
"io"
"strings"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
const (
maxRed = 12
maxGreen = 13
maxBlue = 14
)
scanner := bufio.NewScanner(input)
possibleCount := 0
game := 0
outer:
for scanner.Scan() {
game++
line := scanner.Text()
idx := strings.Index(line, ": ")
line = line[idx+2:]
del := aoc.NewDelimiter(line, "; ")
for _, set := range del.GetStrings() {
del2 := aoc.NewDelimiter(set, ", ")
for _, cube := range del2.GetStrings() {
del3 := aoc.NewDelimiter(cube, " ")
count := del3.GetInt(0)
color := del3.GetString(1)
switch color {
case "red":
if count > maxRed {
continue outer
}
case "green":
if count > maxGreen {
continue outer
}
case "blue":
if count > maxBlue {
continue outer
}
default:
panic(color)
}
}
}
possibleCount += game
}
return possibleCount
}
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
res := 0
game := 0
for scanner.Scan() {
game++
line := scanner.Text()
idx := strings.Index(line, ": ")
line = line[idx+2:]
maxRed := 0
maxGreen := 0
maxBlue := 0
del := aoc.NewDelimiter(line, "; ")
for _, set := range del.GetStrings() {
del2 := aoc.NewDelimiter(set, ", ")
for _, cube := range del2.GetStrings() {
del3 := aoc.NewDelimiter(cube, " ")
count := del3.GetInt(0)
color := del3.GetString(1)
switch color {
case "red":
maxRed = max(maxRed, count)
case "green":
maxGreen = max(maxGreen, count)
case "blue":
maxBlue = max(maxBlue, count)
default:
panic(color)
}
}
}
res += maxRed * maxGreen * maxBlue
}
return res
}