|
|
|
@ -4,13 +4,29 @@ import java.io.File |
|
|
|
|
|
|
|
object Day2 { |
|
|
|
|
|
|
|
fun readGames(file: File = File("src/main/resources/day2/input.txt")): List<Game> = file |
|
|
|
.readLines() |
|
|
|
.map(Game.Companion::parse) |
|
|
|
fun sumOfCompatibleGames(hand: Hand) = readGames() |
|
|
|
.filter { it.isCompatibleWith(hand) } |
|
|
|
.map(Game::id) |
|
|
|
.sum() |
|
|
|
|
|
|
|
private fun readGames(): List<Game> = |
|
|
|
File("src/main/resources/day2/input.txt") |
|
|
|
.readLines() |
|
|
|
.map(Game.Companion::parse) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
data class Game(val id: Int, val hands: List<Hand>) { |
|
|
|
fun isCompatibleWith(hand: Hand) = |
|
|
|
hand.fitsIn( |
|
|
|
hands.fold(Hand(0, 0, 0)) { currentMax: Hand, next: Hand -> |
|
|
|
Hand( |
|
|
|
maxOf(currentMax.red, next.red), |
|
|
|
maxOf(currentMax.green, next.green), |
|
|
|
maxOf(currentMax.blue, next.blue) |
|
|
|
) |
|
|
|
}) |
|
|
|
|
|
|
|
companion object { |
|
|
|
|
|
|
|
/** |
|
|
|
@ -30,6 +46,8 @@ data class Game(val id: Int, val hands: List) { |
|
|
|
} |
|
|
|
|
|
|
|
data class Hand(val red: Int, val green: Int, val blue: Int) { |
|
|
|
fun fitsIn(other: Hand) = red >= other.red && green >= other.green && blue >= other.blue |
|
|
|
|
|
|
|
companion object { |
|
|
|
|
|
|
|
/** |
|
|
|
|