day(06): do not recreate list every time
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
0df8adcc08
commit
eb3803e942
1 changed files with 4 additions and 8 deletions
|
@ -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()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue