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 import readInputAsCommaSeparatedInts
fun howManyAfter(input: List<Int>, days: Int): Long = fun howManyAfter(input: List<Int>, days: Int): Long =
(1..days) (0 until days)
.fold(List(9) { i -> input.count { it == i }.toLong() }) { counts, _ -> .fold(MutableList(9) { i -> input.count { it == i }.toLong() }) { counts, day ->
List(9) { i -> counts[(7 + day) % 9] += counts[day % 9]
when (i) { counts
6 -> counts[0] + counts[7]
else -> counts[(i + 1) % 9]
}
}
} }
.sum() .sum()