Browse Source

Day 2: Unit test

master
Jeremy Soumokil 2 years ago
parent
commit
64b020ff65
2 changed files with 17 additions and 1 deletions
  1. +12
    -0
      src/main/kotlin/day2/Day2.kt
  2. +5
    -1
      src/test/kotlin/day2/Day2Test.kt

+ 12
- 0
src/main/kotlin/day2/Day2.kt 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) {
}
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
src/test/kotlin/day2/Day2Test.kt 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")
}
}

Loading…
Cancel
Save