Skip to content

Commit

Permalink
Solve 2023 day 23 using key neighbors
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 23, 2023
1 parent 4946473 commit d4e8245
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
63 changes: 50 additions & 13 deletions src/main/scala/eu/sim642/adventofcode2023/Day23.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package eu.sim642.adventofcode2023

import eu.sim642.adventofcodelib.Grid
import eu.sim642.adventofcodelib.graph.{BFS, GraphTraversal, UnitNeighbors}
import eu.sim642.adventofcodelib.graph.{BFS, Dijkstra, GraphTraversal, UnitNeighbors}
import eu.sim642.adventofcodelib.pos.Pos
import eu.sim642.adventofcodelib.GridImplicits.*
import eu.sim642.adventofcodelib.box.Box

object Day23 {

case class HikePos(pos: Pos, path: Set[Pos])
case class HikePos(pos: Pos, path: List[Pos], pathSet: Set[Pos])

private val slopeOffsets = Map(
'^' -> Pos(0, -1),
Expand All @@ -18,27 +19,63 @@ object Day23 {

def longestHike(grid: Grid[Char], slopes: Boolean = true): Int = {

val graphTraversal = new GraphTraversal[HikePos] with UnitNeighbors[HikePos] {
override val startNode: HikePos = HikePos(Pos(1, 0), Set(Pos(1, 0))) // TODO: don't hardcode
val startPos = Pos(1, 0) // TODO: don't hardcode
val targetPos = Pos(grid(0).size - 2, grid.size - 1)

val branchPoss = (for {
pos <- Box(Pos(1, 1), Pos(grid(0).size - 2, grid.size - 2)).iterator
if grid(pos) != '#'
if Pos.axisOffsets.count(offset => grid(pos + offset) != '#') > 2
} yield pos).toSet

val keyPoss = branchPoss + startPos + targetPos
println(keyPoss)

val keyNeighbors: Map[Pos, Map[Pos, Int]] = {
keyPoss.view.map({ fromPos =>

val graphTraversal = new GraphTraversal[Pos] with UnitNeighbors[Pos] {
override val startNode: Pos = fromPos

override def unitNeighbors(hikePos: HikePos): IterableOnce[HikePos] = {
val HikePos(pos, path) = hikePos
override def unitNeighbors(pos: Pos): IterableOnce[Pos] = {
if (pos != fromPos && keyPoss(pos))
Iterator.empty
else {
for {
offset <- if (!slopes || grid(pos) == '.') Pos.axisOffsets else Seq(slopeOffsets(grid(pos)))
newPos = pos + offset
if grid.containsPos(newPos)
if grid(newPos) != '#'
} yield newPos
}
}
}

val distances = BFS.traverse(graphTraversal).distances.toMap
fromPos -> distances.filter(p => p._1 != fromPos && keyPoss(p._1))
}).toMap
}

val graphTraversal = new GraphTraversal[HikePos] {
override val startNode: HikePos = HikePos(startPos, List(startPos), Set(startPos))

override def neighbors(hikePos: HikePos): IterableOnce[(HikePos, Int)] = {
val HikePos(pos, path, pathSet) = hikePos
for {
offset <- if (!slopes || grid(pos) == '.') Pos.axisOffsets else Seq(slopeOffsets(grid(pos)))
newPos = pos + offset
if grid.containsPos(newPos)
if grid(newPos) != '#'
(newPos, dist) <- keyNeighbors(pos)
//() = println((pos, newPos, dist))
//() = assert(slopes || keyNeighbors(newPos)(pos) == dist)
if !path.contains(newPos)
} yield HikePos(newPos, path + newPos)
} yield HikePos(newPos, newPos :: path, pathSet + newPos) -> dist
}
}

val targetPos = Pos(grid(0).size - 2, grid.size - 1)

val asd = BFS.traverse(graphTraversal).distances
val asd = Dijkstra.traverse(graphTraversal).distances
.filter(_._1.pos == targetPos)

asd.values.toSet.foreach(println)
println(asd.maxBy(_._2)._1.path)
asd
.values
.max
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/eu/sim642/adventofcode2023/Day23Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Day23Test extends AnyFunSuite {
assert(longestHike(parseGrid(exampleInput)) == 94)
}

ignore("Part 1 input answer") { // TODO: optimize
test("Part 1 input answer") { // TODO: optimize
assert(longestHike(parseGrid(input)) == 2414)
}

Expand All @@ -43,6 +43,6 @@ class Day23Test extends AnyFunSuite {
}

ignore("Part 2 input answer") { // TODO: optimize
assert(longestHike(parseGrid(input), slopes = false) == 2414)
//assert(longestHike(parseGrid(input), slopes = false) == 2414)
}
}

0 comments on commit d4e8245

Please sign in to comment.