day(06): refactor getting initial position

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-12-06 15:18:24 +01:00
parent b78c91e5db
commit d62bdd1807
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -6,20 +6,20 @@ class Day06(
private val map: Array<CharArray> = 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<Vector> =
guard(null).let { (visited, _) ->
visited.map { (_, p) -> p }.toSet()
}
private val route: Set<Vector> = guard(null).let { (visited, _) -> visited.map { (_, p) -> p }.toSet() }
override fun precompute() {
// no-op
}