Skip to content

Commit

Permalink
Simplify 2024 day 18 part 2 linear on path solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 19, 2024
1 parent 45a5ef6 commit 0e4fe48
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/main/scala/eu/sim642/adventofcode2024/Day18.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ object Day18 {

@tailrec
def helper(after: Int, path: Set[Pos]): Int = {
if (path(bytes(after))) {
exitPath(bytes, max, after + 1) match {
case Some(newPath) => helper(after + 1, newPath.toSet)
case None => after + 1
}
val newAfter = bytes.indexWhere(path, after) + 1
assert(newAfter >= 1) // indexWhere didn't return -1
exitPath(bytes, max, newAfter) match {
case Some(newPath) => helper(newAfter, newPath.toSet)
case None => newAfter
}
else
helper(after + 1, path)
}

val blockingAfter = helper(0, exitPath(bytes, max, 0).get.toSet)
Expand Down

0 comments on commit 0e4fe48

Please sign in to comment.