Skip to content

Commit

Permalink
Optimize BFS.searchPaths path construction
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 19, 2024
1 parent 5ed90a9 commit 45a5ef6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/eu/sim642/adventofcode2024/Day18.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object Day18 {
object LinearOnPathPart2Solution extends Part2Solution {
def exitPath(bytes: Seq[Pos], max: Pos, after: Int): Option[Seq[Pos]] = {
val graphSearch = bytesGraphSearch(bytes, max, after + 1)
BFS.searchPaths(graphSearch).paths.get(graphSearch.targetNode) // TODO: optimize paths to not compute everything
BFS.searchPaths(graphSearch).paths.lift(graphSearch.targetNode)
}

override def findBlockingByte(bytes: Seq[Pos], max: Pos = Pos(70, 70)): Pos = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ trait Distances[A] {
trait Paths[A] {
def prevNodes: collection.Map[A, A]

def paths: collection.Map[A, Seq[A]] = {
prevNodes.map((node, _) =>
node -> (node +: LazyList.unfold0(node)(prevNodes.get)).reverse
def paths: PartialFunction[A, Seq[A]] =
prevNodes.andThen(node =>
(node #:: LazyList.unfold0(node)(prevNodes.get)).reverse // TODO: don't bother with LazyList now that it's a function
)
}

/*def paths(node: A): Seq[A] =
(node +: LazyList.unfold0(node)(prevNodes.get)).reverse*/
}

trait Order[A] {
Expand Down

0 comments on commit 45a5ef6

Please sign in to comment.