|
|
@ -9,6 +9,10 @@ object Day2 { |
|
|
.map(Game::id) |
|
|
.map(Game::id) |
|
|
.sum() |
|
|
.sum() |
|
|
|
|
|
|
|
|
|
|
|
fun powerOfMinimumSetOfCubes(file: String) = readGames(file) |
|
|
|
|
|
.map(Game::powerOfHands) |
|
|
|
|
|
.sum() |
|
|
|
|
|
|
|
|
private fun readGames(file: String): List<Game> = |
|
|
private fun readGames(file: String): List<Game> = |
|
|
File(file) |
|
|
File(file) |
|
|
.readLines() |
|
|
.readLines() |
|
|
@ -17,15 +21,20 @@ object Day2 { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
data class Game(val id: Int, val hands: List<Hand>) { |
|
|
data class Game(val id: Int, val hands: List<Hand>) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun isCompatibleWith(hand: 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) |
|
|
|
|
|
) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
hand.fitsIn(maxOfHands()) |
|
|
|
|
|
|
|
|
|
|
|
fun powerOfHands() = maxOfHands().let { it.red * it.green * it.blue } |
|
|
|
|
|
|
|
|
|
|
|
private fun maxOfHands() = 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 { |
|
|
companion object { |
|
|
|
|
|
|
|
|
|