Skip to content

Commit 0e4fe48

Browse files
committed
Simplify 2024 day 18 part 2 linear on path solution
1 parent 45a5ef6 commit 0e4fe48

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/main/scala/eu/sim642/adventofcode2024/Day18.scala

+5-7
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,12 @@ object Day18 {
6464

6565
@tailrec
6666
def helper(after: Int, path: Set[Pos]): Int = {
67-
if (path(bytes(after))) {
68-
exitPath(bytes, max, after + 1) match {
69-
case Some(newPath) => helper(after + 1, newPath.toSet)
70-
case None => after + 1
71-
}
67+
val newAfter = bytes.indexWhere(path, after) + 1
68+
assert(newAfter >= 1) // indexWhere didn't return -1
69+
exitPath(bytes, max, newAfter) match {
70+
case Some(newPath) => helper(newAfter, newPath.toSet)
71+
case None => newAfter
7272
}
73-
else
74-
helper(after + 1, path)
7573
}
7674

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

0 commit comments

Comments
 (0)