day(06): make it functional
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
d7cde13a2e
commit
0df8adcc08
1 changed files with 10 additions and 13 deletions
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue