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