Compare commits
2
Commits
a975d18556
...
a70d9fafee
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a70d9fafee | ||
|
|
64b020ff65 |
No files matched your search
@@ -4,13 +4,29 @@ import java.io.File
|
||||
|
||||
object Day2 {
|
||||
|
||||
fun readGames(file: File = File("src/main/resources/day2/input.txt")): List<Game> = file
|
||||
.readLines()
|
||||
.map(Game.Companion::parse)
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
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 +46,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 {
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,6 +6,6 @@ class Day2Test {
|
||||
|
||||
@Test
|
||||
fun readGames() {
|
||||
Day2.readGames()
|
||||
assert(Day2.sumOfCompatibleGames(Hand(12, 13, 14)) == 8)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user