1
0
Fork 0

day(06): make it functional

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-06 10:37:13 +01:00
parent d7cde13a2e
commit 0df8adcc08

View file

@ -2,20 +2,17 @@ package day06
import readInputAsCommaSeparatedInts
fun howManyAfter(input: List<Int>, days: Int): Long {
var counts = List(9) { i -> input.count { it == i }.toLong() }
for (day in 1..days) {
counts = List(9) { i ->
when (i) {
6 -> counts[0] + counts[7]
else -> counts[(i + 1) % 9]
fun howManyAfter(input: List<Int>, days: Int): Long =
(1..days)
.fold(List(9) { i -> input.count { it == i }.toLong() }) { counts, _ ->
List(9) { i ->
when (i) {
6 -> counts[0] + counts[7]
else -> counts[(i + 1) % 9]
}
}
}
}
return counts.sum()
}
.sum()
fun main() {
val testInput = readInputAsCommaSeparatedInts(6, "test_input")
@ -23,7 +20,7 @@ fun main() {
check(howManyAfter(testInput, 80) == 5934.toLong())
println(howManyAfter(input, 80))
check(howManyAfter(testInput, 256) == 26984457539)
println(howManyAfter(input, 256))
}