1
0
Fork 0

day(06): do not recreate list every time

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-06 11:15:39 +01:00
parent 0df8adcc08
commit eb3803e942

View file

@ -3,14 +3,10 @@ package day06
import readInputAsCommaSeparatedInts
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]
}
}
(0 until days)
.fold(MutableList(9) { i -> input.count { it == i }.toLong() }) { counts, day ->
counts[(7 + day) % 9] += counts[day % 9]
counts
}
.sum()