Day 2: Sum calculation in program

This commit is contained in:
jeremy committed 2023-12-03 00:41:30 +01:00
1 parent 64b020ff65
commit a70d9fafee
2 files changed
+8 -6

No files matched your search

+7 -1
View File
@@ -4,7 +4,13 @@ import java.io.File
object Day2 {
fun readGames(file: File = File("src/main/resources/day2/input.txt")): List<Game> = file
fun sumOfCompatibleGames(hand: Hand) = readGames()
.filter { it.isCompatibleWith(hand) }
.map(Game::id)
.sum()
private fun readGames(): List<Game> =
File("src/main/resources/day2/input.txt")
.readLines()
.map(Game.Companion::parse)
+1 -5
View File
@@ -6,10 +6,6 @@ class Day2Test {
@Test
fun readGames() {
val compatibleGames = Day2.readGames().filter { game ->
Hand(12, 13, 14).let(game::isCompatibleWith)
}
assert(compatibleGames.map(Game::id).joinToString(separator=",") == "1,2,5")
assert(Day2.sumOfCompatibleGames(Hand(12, 13, 14)) == 8)
}
}