From d62bdd18076c633ca1e52105e37063a9be100c35 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 6 Dec 2024 15:18:24 +0100 Subject: [PATCH] day(06): refactor getting initial position Signed-off-by: Matej Focko --- src/Day06.kt | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Day06.kt b/src/Day06.kt index aeb5230..2fa604e 100644 --- a/src/Day06.kt +++ b/src/Day06.kt @@ -6,20 +6,20 @@ class Day06( private val map: Array = readInput(6, inputType).map { it.toCharArray() }.toTypedArray() private val initialPosition: Vector = - let { - for (row in map.withIndex()) { - for (cell in row.value.withIndex()) { - if (cell.value == '^') { - return@let cell.index to row.index - } + map + .withIndex() + .flatMap { row -> + row.value.withIndex().map { cell -> + (cell.index to row.index) to cell.value } - } + }.first { (_, cell) -> cell == '^' } + .let { (pos, _) -> pos } - error("there's always at least one guard") + private val route: Set = + guard(null).let { (visited, _) -> + visited.map { (_, p) -> p }.toSet() } - private val route: Set = guard(null).let { (visited, _) -> visited.map { (_, p) -> p }.toSet() } - override fun precompute() { // no-op }