-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/me/peckb/aoc/_2023/calendar/day02/Day02.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## [Day 2: Cube Conundrum](https://adventofcode.com/2023/day/2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/kotlin/me/peckb/aoc/_2023/calendar/day02/Day02Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |