Day 2: Unit test
This commit is contained in:
1 parent
a975d18556
commit
64b020ff65
2 files changed
+17
-1
No files matched your search
@@ -11,6 +11,16 @@ 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) =
|
||||||
|
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 {
|
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) {
|
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 {
|
companion object {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ class Day2Test {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun readGames() {
|
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user