-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (76 loc) · 1.66 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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
/**
* Day 2: Cube Conundrum
* url: https://adventofcode.com/2023/day/2
*/
func main() {
sum := 0
sum2 := 0
lines := findMaxColors("input.txt")
// For Testing
// lines := findMaxColors("input_test.txt")
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)
}
/**
* Find the maximum number of cubes of each color for each line of a file
*/
func findMaxColors(filename string) [][]int {
bytesRead, _ := os.ReadFile(filename)
fileContent := string(bytesRead)
lines := strings.Split(fileContent, "\n")
colorMaximums := [][]int{}
for _, line := range lines {
line = strings.TrimSpace(line)
args := strings.Split(line, ":")
if len(args) != 2 {
fmt.Println("Invalid input", args)
return nil
}
tries := strings.Split(args[1], ";")
lineMax := []int{0, 0, 0}
for _, try := range tries {
cubes := strings.Split(try, ",")
for _, cube := range cubes {
val := strings.Split(strings.TrimSpace(cube), " ")
num, _ := strconv.Atoi(val[0])
color := parseColor(val[1])
if color == -1 {
fmt.Println("Invalid input", args)
return nil
}
lineMax[color] = max(lineMax[color], num)
}
}
colorMaximums = append(colorMaximums, lineMax)
}
return colorMaximums
}
func parseColor(color string) int {
switch color {
case "red":
return 0
case "blue":
return 1
case "green":
return 2
default:
return -1
}
}