From 2d68999fdabab018d8409d1753aa52ccf819ab12 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 4 Dec 2024 11:40:57 +0100 Subject: [PATCH] day(04): prefilter indices in the second part MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows to get rid of the ‹when› expressions that just introduces another level of indentation. Signed-off-by: Matej Focko --- src/Day04.kt | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Day04.kt b/src/Day04.kt index f2ea3dd..b337f29 100644 --- a/src/Day04.kt +++ b/src/Day04.kt @@ -38,18 +38,18 @@ class Day04( } override fun part2(): Int = - product(wordSearch.indices, wordSearch[0].indices).sumOf { (y, x) -> - when (wordSearch[y][x]) { - 'A' -> - NEIGHBORING_DIRECTIONS.count { (d0, d1) -> - val (dy0, dx0) = d0 - val (dy1, dx1) = d1 - startsWith("MAS", y - dy0, x - dx0, dy0, dx0) && startsWith("MAS", y - dy1, x - dx1, dy1, dx1) - } - - else -> 0 + product(wordSearch.indices, wordSearch[0].indices) + .filter { (y, x) -> wordSearch[y][x] == 'A' } + .sumOf { (y, x) -> + NEIGHBORING_DIRECTIONS.count { (d0, d1) -> + val (dy0, dx0) = d0 + val (dy1, dx1) = d1 + ( + startsWith("MAS", y - dy0, x - dx0, dy0, dx0) && + startsWith("MAS", y - dy1, x - dx1, dy1, dx1) + ) + } } - } } fun main() {