Skip to content

Commit

Permalink
Day 02 2025 (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
peckb1 authored Dec 2, 2023
1 parent b1aca9f commit b516c70
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/main/kotlin/me/peckb/aoc/_2023/calendar/day02/Day02.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.peckb.aoc._2023.calendar.day02

import javax.inject.Inject

import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
import kotlin.math.max

class Day02 @Inject constructor(
private val generatorFactory: InputGeneratorFactory,
) {
fun partOne(filename: String) = generatorFactory.forFile(filename).readAs(::game) { games ->
val maxRed = 12
val maxGreen = 13
val maxBlue = 14

games.filter { game ->
game.rounds.none { round ->
round.red > maxRed || round.blue > maxBlue || round.green > maxGreen
}
}.sumOf { it.id }
}

fun partTwo(filename: String) = generatorFactory.forFile(filename).readAs(::game) { games ->
games.sumOf { game ->
var minRequiredRed = 0
var minRequiredBlue = 0
var minRequiredGreen = 0

game.rounds.forEach { round ->
minRequiredRed = max(minRequiredRed, round.red)
minRequiredBlue = max(minRequiredBlue, round.blue)
minRequiredGreen = max(minRequiredGreen, round.green)
}

minRequiredRed * minRequiredGreen * minRequiredBlue
}
}

private fun game(line: String): Game {
val (gameIdString, roundData) = line.split(": ")
val id = gameIdString.split(" ").last().toInt()

val roundStrings = roundData.split("; ")

val rounds = roundStrings.map { roundInformation ->
var red = 0
var blue = 0
var green = 0

roundInformation.split(", ")
.forEach { colourInformation ->
val (count, colour) = colourInformation.split(" ")
when (colour) {
"red" -> red = count.toInt()
"blue" -> blue = count.toInt()
"green" -> green = count.toInt()
}
}

Round(red, green, blue)
}

return Game(id, rounds)
}

data class Game(val id: Int, val rounds: List<Round>)

data class Round(val red: Int, val green: Int, val blue: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## [Day 2: Cube Conundrum](https://adventofcode.com/2023/day/2)
2 changes: 2 additions & 0 deletions src/test/kotlin/me/peckb/aoc/_2023/TestDayComponent.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.peckb.aoc._2023

import dagger.Component
import me.peckb.aoc._2023.calendar.day02.Day02Test
import javax.inject.Singleton

import me.peckb.aoc.DayComponent
Expand All @@ -11,4 +12,5 @@ import me.peckb.aoc._2023.calendar.day01.Day01Test
@Component(modules = [InputModule::class])
internal interface TestDayComponent : DayComponent {
fun inject(day01Test: Day01Test)
fun inject(day02Test: Day02Test)
}
33 changes: 33 additions & 0 deletions src/test/kotlin/me/peckb/aoc/_2023/calendar/day02/Day02Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.peckb.aoc._2023.calendar.day02

import javax.inject.Inject


import me.peckb.aoc._2023.DaggerTestDayComponent
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

internal class Day02Test {
@Inject
lateinit var day02: Day02

@BeforeEach
fun setup() {
DaggerTestDayComponent.create().inject(this)
}

@Test
fun testDay02PartOne() {
assertEquals(2563, day02.partOne(DAY_02))
}

@Test
fun testDay02PartTwo() {
assertEquals(70768, day02.partTwo(DAY_02))
}

companion object {
private const val DAY_02: String = "advent-of-code-input/2023/day02.input"
}
}

0 comments on commit b516c70

Please sign in to comment.