Skip to content

Commit

Permalink
Add solutions for 2023 Day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Dec 2, 2023
1 parent 1bc7b3c commit 1ee4ba9
Show file tree
Hide file tree
Showing 5 changed files with 1,203 additions and 1,001 deletions.
79 changes: 79 additions & 0 deletions 2023/02p1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
21 changes: 21 additions & 0 deletions 2023/02p2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"

"aoc2023/utils"
)

func D02P2() {
lines := utils.ReadLines("inputs/02.txt")
games := []cubeGame{}

powerLevelSum := 0

for i, line := range lines {
games = append(games, parseCubeGameLine(line))
gamePowerLevel := games[i].minimumRed * games[i].minimumGreen * games[i].minimumBlue
powerLevelSum += gamePowerLevel
}
fmt.Printf("Total Power Level: %d\n", powerLevelSum)
}
Loading

0 comments on commit 1ee4ba9

Please sign in to comment.