From 0df8adcc08f582f87b4ff7a0ff5524f1829de130 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 6 Dec 2021 10:37:13 +0100 Subject: [PATCH] day(06): make it functional Signed-off-by: Matej Focko --- src/day06/Day06.kt | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/day06/Day06.kt b/src/day06/Day06.kt index 8b56d4a..f258670 100644 --- a/src/day06/Day06.kt +++ b/src/day06/Day06.kt @@ -2,20 +2,17 @@ package day06 import readInputAsCommaSeparatedInts -fun howManyAfter(input: List, 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, 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") @@ -23,7 +20,7 @@ fun main() { check(howManyAfter(testInput, 80) == 5934.toLong()) println(howManyAfter(input, 80)) - + check(howManyAfter(testInput, 256) == 26984457539) println(howManyAfter(input, 256)) }