Skip to content

Commit

Permalink
Try BFS with multiple start nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 23, 2024
1 parent ad4578f commit fcd5795
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/eu/sim642/adventofcode2024/Day16.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ object Day16 {
val forwardSearch = forwardGraphSearch(grid)
val forwardResult = Dijkstra.search(forwardSearch)

val backwardTraversal = new GraphTraversal[Reindeer] with UnitNeighbors[Reindeer] {
override val startNode: Reindeer = forwardResult.target.get._1 // TODO: other orientations
val backwardTraversal = new GraphTraversal0[Reindeer] with UnitNeighbors[Reindeer] {
override def startNodes: IterableOnce[Reindeer] = Pos.axisOffsets.map(d => forwardResult.target.get._1.copy(direction = d)).filter(forwardResult.nodes.contains)

override def unitNeighbors(reindeer: Reindeer): IterableOnce[Reindeer] = {
val distance = forwardResult.distances(reindeer)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/eu/sim642/adventofcodelib/graph/BFS.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ object BFS {
// TODO: reduce duplication without impacting performance

// copied from Dijkstra
def traverse[A](graphTraversal: GraphTraversal[A] & UnitNeighbors[A]): Distances[A] = {
def traverse[A](graphTraversal: GraphTraversal0[A] & UnitNeighbors[A]): Distances[A] = {
val visitedDistance: mutable.Map[A, Int] = mutable.Map.empty
val toVisit: mutable.Queue[(Int, A)] = mutable.Queue.empty

def enqueue(node: A, dist: Int): Unit = {
toVisit.enqueue((dist, node))
}

enqueue(graphTraversal.startNode, 0)
graphTraversal.startNodes.iterator.foreach(enqueue(_, 0))

while (toVisit.nonEmpty) {
val (dist, node) = toVisit.dequeue()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package eu.sim642.adventofcodelib.graph

trait GraphSearch[A] extends GraphTraversal[A] {
trait GraphSearch0[A] extends GraphTraversal0[A] {
//def isTargetNode(node: A): Boolean
def isTargetNode(node: A, dist: Int): Boolean // TODO: does dist-based target make sense for A*?
}

trait GraphSearch[A] extends GraphTraversal[A], GraphSearch0[A]

trait TargetNode[A] { this: GraphSearch[A] =>
val targetNode: A

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package eu.sim642.adventofcodelib.graph

import eu.sim642.adventofcodelib.LazyListImplicits._

trait GraphTraversal[A] {
trait GraphTraversal0[A] {
def startNodes: IterableOnce[A] // TODO: should be val?
def neighbors(node: A): IterableOnce[(A, Int)]
}

trait GraphTraversal[A] extends GraphTraversal0[A] {
val startNode: A

override def startNodes: IterableOnce[A] = Iterator.single(startNode)

def neighbors(node: A): IterableOnce[(A, Int)]
}

trait UnitNeighbors[A] { this: GraphTraversal[A] =>
trait UnitNeighbors[A] { this: GraphTraversal0[A] =>
def unitNeighbors(node: A): IterableOnce[A]

override final def neighbors(node: A): IterableOnce[(A, Int)] = unitNeighbors(node).iterator.map(_ -> 1)
Expand Down

0 comments on commit fcd5795

Please sign in to comment.