-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02p1.go
79 lines (64 loc) · 1.68 KB
/
02p1.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
package main
import (
"fmt"
"strconv"
"strings"
"aoc2023/utils"
)
type cubeGame struct {
id int
minimumRed int
minimumGreen int
minimumBlue int
possible bool
}
func D02P1() {
lines := utils.ReadLines("inputs/02.txt")
games := []cubeGame{}
idTotal := 0
for i, line := range lines {
games = append(games, parseCubeGameLine(line))
games[i].possible = checkGamePossibility(games[i], 12, 13, 14)
if games[i].possible {
idTotal += games[i].id
}
}
fmt.Printf("Total of possible games: %d\n", idTotal)
}
func parseCubeGameLine(line string) cubeGame {
currentGame := cubeGame{}
// Parse the game ID
identityString := strings.Split(line, ":")[0]
currentGame.id, _ = strconv.Atoi(strings.Split(identityString, " ")[1])
gameOutcome := line[7:]
handfulls := strings.Split(gameOutcome, ";")
// Split the handfulls into colours and counts
for _, handfull := range handfulls {
colours := strings.Split(handfull, ",")
for _, colour := range colours {
count, _ := strconv.Atoi(strings.Split(colour, " ")[1])
colourName := strings.Split(colour, " ")[2]
switch colourName {
case "red":
if count > currentGame.minimumRed {
currentGame.minimumRed = count
}
case "green":
if count > currentGame.minimumGreen {
currentGame.minimumGreen = count
}
case "blue":
if count > currentGame.minimumBlue {
currentGame.minimumBlue = count
}
}
}
}
return currentGame
}
func checkGamePossibility(game cubeGame, red, green, blue int) bool {
redPossible := game.minimumRed <= red
greenPossible := game.minimumGreen <= green
bluePossible := game.minimumBlue <= blue
return redPossible && greenPossible && bluePossible
}