-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.go
106 lines (85 loc) · 1.52 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
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
_ "embed"
"fmt"
"strconv"
"strings"
)
//go:embed input.txt
var input string
const (
MAX_RED = 12
MAX_GREEN = 13
MAX_BLUE = 14
)
func part1(input string) int {
lines := strings.Split(input, "\n")
var sum int
for lineIndex, line := range lines {
_, line, _ = strings.Cut(line, ": ")
isPossible := true
var start int
for i := 1; i < len(line); i++ {
if line[i] == ' ' {
amount, _ := strconv.Atoi(line[start:i])
var r, g, b int
switch line[i+1] {
case 'r':
r = amount
i += 6
start = i
case 'g':
g = amount
i += 8
start = i
case 'b':
b = amount
i += 7
start = i
}
if r > MAX_RED || g > MAX_GREEN || b > MAX_BLUE {
isPossible = false
break
}
}
}
if isPossible {
sum += lineIndex + 1
}
}
return sum
}
func part2(input string) int {
lines := strings.Split(input, "\n")
var sum int
for _, line := range lines {
_, line, _ = strings.Cut(line, ": ")
var start, r, g, b int
for i := 1; i < len(line); i++ {
if line[i] == ' ' {
amount, _ := strconv.Atoi(line[start:i])
switch line[i+1] {
case 'r':
r = max(r, amount)
i += 6
start = i
case 'g':
g = max(g, amount)
i += 8
start = i
case 'b':
b = max(b, amount)
i += 7
start = i
}
}
}
sum += r * g * b
}
return sum
}
func main() {
fmt.Println("--- 2023 day 02 answer ---")
fmt.Println("part 1:\t", part1(input))
fmt.Println("part 2:\t", part2(input))
}