day(04): prefilter indices in the second part

Allows to get rid of the ‹when› expressions that just introduces another
level of indentation.

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-12-04 11:40:57 +01:00
parent 8730cbd423
commit 2d68999fda
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -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() {