From 64b020ff65da3aeae019a56ffb98028006af219c Mon Sep 17 00:00:00 2001 From: Jeremy Soumokil Date: Sun, 3 Dec 2023 00:33:24 +0100 Subject: [PATCH] Day 2: Unit test --- src/main/kotlin/day2/Day2.kt | 12 ++++++++++++ src/test/kotlin/day2/Day2Test.kt | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/day2/Day2.kt b/src/main/kotlin/day2/Day2.kt index 0ca8679..2b782ea 100644 --- a/src/main/kotlin/day2/Day2.kt +++ b/src/main/kotlin/day2/Day2.kt @@ -11,6 +11,16 @@ object Day2 { } data class Game(val id: Int, val hands: List) { + 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) { } 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 { /** diff --git a/src/test/kotlin/day2/Day2Test.kt b/src/test/kotlin/day2/Day2Test.kt index 27377da..5ca0729 100644 --- a/src/test/kotlin/day2/Day2Test.kt +++ b/src/test/kotlin/day2/Day2Test.kt @@ -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") } } \ No newline at end of file