Day 2: Unit test

This commit is contained in:
jeremy committed 2023-12-03 00:34:22 +01:00
1 parent a975d18556
commit 64b020ff65
2 files changed
+17 -1

No files matched your search

+12
View File
@@ -11,6 +11,16 @@ object Day2 {
}
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 +40,8 @@ data class Game(val id: Int, val hands: List<Hand>) {
}
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 {
/**
+5 -1
View File
@@ -6,6 +6,10 @@ class Day2Test {
@Test
fun readGames() {
Day2.readGames()
val compatibleGames = Day2.readGames().filter { game ->
Hand(12, 13, 14).let(game::isCompatibleWith)
}
assert(compatibleGames.map(Game::id).joinToString(separator=",") == "1,2,5")
}
}