-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_v2.go
63 lines (51 loc) · 1.19 KB
/
main_v2.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
package main
import (
_ "embed"
"fmt"
"regexp"
"strconv"
"strings"
)
/**
* Day 2: Cube Conundrum
* url: https://adventofcode.com/2023/day/2
*
* Version 2: Try to improve a bit my code
* - use embed to read the input file
* - use regexp to extract values
* - use map resolve colors <> int
*/
//go:embed input.txt
var input string
func main() {
sum := 0
sum2 := 0
lines := findMaxColors()
for idx, line := range lines {
// Part 1
max := []int{12, 14, 13} // Red, Blue, Green
if line[0] <= max[0] && line[1] <= max[1] && line[2] <= max[2] {
sum += idx + 1
}
// Part 2
val := line[0] * line[1] * line[2]
sum2 += val
}
fmt.Println("Part 1:", sum)
fmt.Println("Part 2:", sum2)
}
func findMaxColors() [][]int {
re := regexp.MustCompile(`(\d+) (\w+)`)
colors := map[string]int{"red": 0, "blue": 1, "green": 2}
colorMaximums := [][]int{}
for _, s := range strings.Split(strings.TrimSpace(input), "\n") {
lineMax := []int{0, 0, 0}
for _, val := range re.FindAllStringSubmatch(s, -1) {
num, _ := strconv.Atoi(val[1])
color := colors[val[2]]
lineMax[color] = max(lineMax[color], num)
}
colorMaximums = append(colorMaximums, lineMax)
}
return colorMaximums
}