Skip to content

Commit 45a5ef6

Browse files
committed
Optimize BFS.searchPaths path construction
1 parent 5ed90a9 commit 45a5ef6

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object Day18 {
5757
object LinearOnPathPart2Solution extends Part2Solution {
5858
def exitPath(bytes: Seq[Pos], max: Pos, after: Int): Option[Seq[Pos]] = {
5959
val graphSearch = bytesGraphSearch(bytes, max, after + 1)
60-
BFS.searchPaths(graphSearch).paths.get(graphSearch.targetNode) // TODO: optimize paths to not compute everything
60+
BFS.searchPaths(graphSearch).paths.lift(graphSearch.targetNode)
6161
}
6262

6363
override def findBlockingByte(bytes: Seq[Pos], max: Pos = Pos(70, 70)): Pos = {

src/main/scala/eu/sim642/adventofcodelib/graph/GraphTraversal.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ trait Distances[A] {
2323
trait Paths[A] {
2424
def prevNodes: collection.Map[A, A]
2525

26-
def paths: collection.Map[A, Seq[A]] = {
27-
prevNodes.map((node, _) =>
28-
node -> (node +: LazyList.unfold0(node)(prevNodes.get)).reverse
26+
def paths: PartialFunction[A, Seq[A]] =
27+
prevNodes.andThen(node =>
28+
(node #:: LazyList.unfold0(node)(prevNodes.get)).reverse // TODO: don't bother with LazyList now that it's a function
2929
)
30-
}
31-
32-
/*def paths(node: A): Seq[A] =
33-
(node +: LazyList.unfold0(node)(prevNodes.get)).reverse*/
3430
}
3531

3632
trait Order[A] {

0 commit comments

Comments
 (0)