Skip to content

Commit

Permalink
Remove commented-out code in 2024 day 16 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 16, 2024
1 parent 0fb4520 commit 30f7a97
Showing 1 changed file with 3 additions and 90 deletions.
93 changes: 3 additions & 90 deletions src/main/scala/eu/sim642/adventofcode2024/Day16.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package eu.sim642.adventofcode2024

import eu.sim642.adventofcode2018.Day13.DirectionPos
import eu.sim642.adventofcodelib.Grid
import eu.sim642.adventofcodelib.graph.{BFS, Dijkstra, GraphSearch, GraphTraversal, TargetNode, UnitNeighbors}
import eu.sim642.adventofcodelib.pos.Pos
import eu.sim642.adventofcodelib.GridImplicits.*
import eu.sim642.adventofcode2018.Day13.DirectionPos

import scala.collection.mutable
import eu.sim642.adventofcodelib.graph.*
import eu.sim642.adventofcodelib.pos.Pos

object Day16 {

Expand Down Expand Up @@ -36,91 +34,6 @@ object Day16 {
Dijkstra.search(graphSearch).target.get._2
}

/*def bestPathTiles(grid: Grid[Char]): Int = {
val targetScore = lowestScore(grid)
val graphTraversal = new GraphTraversal[(List[Reindeer], Int)] {
override val startNode: (List[Reindeer], Int) = (List(Reindeer(grid.posOf('S'))), 0)
override def neighbors(reindeers: (List[Reindeer], Int)): IterableOnce[((List[Reindeer], Int), Int)] = {
val reindeer = reindeers._1.head
println(reindeers._2)
Seq(
reindeer.copy(pos = reindeer.pos + reindeer.direction) -> 1,
reindeer.copy(direction = reindeer.direction.left) -> 1000,
reindeer.copy(direction = reindeer.direction.right) -> 1000,
)
.filter(reindeer => grid(reindeer._1.pos) != '#')
.filter(p => reindeers._2 + p._2 <= targetScore)
.map(p => (p._1 :: reindeers._1, reindeers._2 + p._2) -> p._2)
}
}
val targetPos: Pos = grid.posOf('E')
Dijkstra.traverse(graphTraversal).nodes.filter(_._1.head.pos == targetPos).filter(_._2 == targetScore)
.tapEach(println)
.flatMap(_._1.map(_.pos)).toSet.size
}*/

/*def bestPathTiles(grid: Grid[Char]): Int = {
val targetScore = lowestScore(grid)
val graphSearch = new GraphSearch[Reindeer] {
override val startNode: Reindeer = Reindeer(grid.posOf('S'))
override def neighbors(reindeer: Reindeer): IterableOnce[(Reindeer, Int)] = {
Seq(
reindeer.copy(pos = reindeer.pos + reindeer.direction) -> 1,
reindeer.copy(direction = reindeer.direction.left) -> 1000,
reindeer.copy(direction = reindeer.direction.right) -> 1000,
)
.filter(reindeer => grid(reindeer._1.pos) != '#')
}
private val targetPos: Pos = grid.posOf('E')
override def isTargetNode(reindeer: Reindeer, dist: Int): Boolean = reindeer.pos == targetPos
}
val targetPos: Pos = grid.posOf('E')
val memo = mutable.Map.empty[(Reindeer, Set[Reindeer], Int), Set[Pos]]
def helper(reindeer: Reindeer, visited: Set[Reindeer], distance: Int): Set[Pos] = {
memo.getOrElseUpdate((reindeer, visited, distance), {
if (distance == targetScore && reindeer.pos == targetPos)
visited.map(_.pos)
else if (distance >= targetScore)
Set.empty
else {
assert(distance < targetScore)
(for {
(newReindeer, step) <- graphSearch.neighbors(reindeer)
if !visited(newReindeer)
newDistance = distance + step
if newDistance <= targetScore
} yield helper(newReindeer, visited + newReindeer, newDistance)).foldLeft(Set.empty)(_ ++ _)
}
})
}
val s = helper(graphSearch.startNode, Set.empty, 0)
println(s)
for ((row, y) <- grid.zipWithIndex) {
for ((cell, x) <- row.zipWithIndex) {
if (s(Pos(x, y)))
print('O')
else
print(cell)
}
println()
}
s.size
}*/

def bestPathTiles(grid: Grid[Char]): Int = {
val forwardSearch = forwardGraphSearch(grid)
val forwardResult = Dijkstra.search(forwardSearch)
Expand Down

0 comments on commit 30f7a97

Please sign in to comment.