-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day02.kt
40 lines (35 loc) · 1.36 KB
/
Day02.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package io.dmitrijs.aoc2023
// Single expression solutions for the sake of fun.
class Day02(private val input: List<String>) {
fun puzzle1() = input.sumOf { line ->
line.split(": ").let { (start, games) ->
games.split("; ").any { game ->
game.split(", ").any { cubes ->
cubes.examine().let { (color, count) ->
count > totalCubes.getValue(color)
}
}
}.let { failed ->
if (failed) 0 else start.substringAfter(" ").toInt()
}
}
}
fun puzzle2() = input.sumOf { line ->
line.split(": ").let { (_, games) ->
mutableMapOf("red" to 0, "green" to 0, "blue" to 0).apply {
games.split("; ").forEach { game ->
game.split(", ").forEach { cubes ->
cubes.examine().let { (color, count) ->
if (count > getValue(color)) put(color, count)
}
}
}
}.values.product()
}
}
private fun String.examine() = substringAfter(" ") to substringBefore(" ").toInt()
private fun Collection<Int>.product() = fold(1) { acc, i -> acc * i }
companion object {
private val totalCubes = mapOf("red" to 12, "green" to 13, "blue" to 14)
}
}