Skip to content

proof-read-2 #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions docs/2023/puzzles/day23.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Next, we need an algorithm for finding junctions that are connected to a given j
<Literate>

```scala
def connectedJunctions(pos: Point)(using maze: Maze) = List.from:
def connectedJunctions(pos: Point)(using maze: Maze): List[(Point, Int)] = List.from:
assert(maze.junctions.contains(pos))
```

Expand All @@ -139,7 +139,9 @@ This `search` helper method walks down a path from a junction while tracking the

```scala
def search(pos: Point, facing: Dir, dist: Int): Option[(Point, Int)] =
if maze.junctions.contains(pos) then Some(pos, dist) else
if maze.junctions.contains(pos) then
Some(pos, dist)
else
val adjacentSearch = for
nextFacing <- LazyList(facing, facing.turnRight, facing.turnLeft)
nextPos <- walk(pos, nextFacing)
Expand Down Expand Up @@ -176,7 +178,9 @@ def parseInput(fileContents: String): Vector[Vector[Char]] =

def longestDownhillHike(using maze: Maze): Int =
def search(pos: Point, dist: Int): Int =
if pos == maze.end then dist else
if pos == maze.end then
dist
else
connectedJunctions(pos).foldLeft(0):
case (max, (n, d)) => max.max(search(n, dist + d))

Expand Down Expand Up @@ -212,20 +216,23 @@ Next, we define an adjacency graph. Since `connectedJunctions` takes slopes into

```scala
val adjacent: Map[Index, List[(Index, Int)]] =
maze.junctions.toList.flatMap: p1 =>
connectedJunctions(p1).flatMap: (p2, d) =>
val forward = indexOf(p1) -> (indexOf(p2), d)
val reverse = indexOf(p2) -> (indexOf(p1), d)
List(forward, reverse)
.groupMap(_._1)(_._2)
maze.junctions.toList
.flatMap: p1 =>
connectedJunctions(p1).flatMap: (p2, d) =>
val forward = indexOf(p1) -> (indexOf(p2), d)
val reverse = indexOf(p2) -> (indexOf(p1), d)
List(forward, reverse)
.groupMap(_(0))(_(1))
```

Finally, we perform a depth-first search that is very similar to what we used in Part 1.
The main differences are that we now use indices of junctions rather than `Point`s representing current position, and we now check adjacent junctions against a BitSet of visited points, which we now track as we search recursively.

```scala
def search(junction: Index, visited: BitSet, totalDist: Int): Int =
if junction == indexOf(maze.end) then totalDist else
if junction == indexOf(maze.end) then
totalDist
else
adjacent(junction).foldLeft(0):
case (longest, (nextJunct, dist)) =>
if visited(nextJunct) then longest else
Expand Down Expand Up @@ -307,13 +314,15 @@ case class Maze(grid: Vector[Vector[Char]]):
case p if this(p) == '<' => p -> Dir.W
end Maze

def connectedJunctions(pos: Point)(using maze: Maze) = List.from:
def connectedJunctions(pos: Point)(using maze: Maze): List[(Point, Int)] = List.from:
def walk(pos: Point, dir: Dir): Option[Point] =
val p = pos.move(dir)
Option.when(maze.walkable(p) && maze.slopes.get(p).forall(_ == dir))(p)

def search(pos: Point, facing: Dir, dist: Int): Option[(Point, Int)] =
if maze.junctions.contains(pos) then Some(pos, dist) else
if maze.junctions.contains(pos) then
Some(pos, dist)
else
val adjacentSearch = for
nextFacing <- LazyList(facing, facing.turnRight, facing.turnLeft)
nextPos <- walk(pos, nextFacing)
Expand All @@ -330,7 +339,9 @@ end connectedJunctions

def longestDownhillHike(using maze: Maze): Int =
def search(pos: Point, dist: Int): Int =
if pos == maze.end then dist else
if pos == maze.end then
dist
else
connectedJunctions(pos).foldLeft(0):
case (max, (n, d)) => max.max(search(n, dist + d))

Expand All @@ -344,15 +355,18 @@ def longestHike(using maze: Maze): Int =
maze.junctions.toList.sortBy(_.dist(maze.start)).zipWithIndex.toMap

val adjacent: Map[Index, List[(Index, Int)]] =
maze.junctions.toList.flatMap: p1 =>
connectedJunctions(p1).flatMap: (p2, d) =>
val forward = indexOf(p1) -> (indexOf(p2), d)
val reverse = indexOf(p2) -> (indexOf(p1), d)
List(forward, reverse)
.groupMap(_._1)(_._2)
maze.junctions.toList
.flatMap: p1 =>
connectedJunctions(p1).flatMap: (p2, d) =>
val forward = indexOf(p1) -> (indexOf(p2), d)
val reverse = indexOf(p2) -> (indexOf(p1), d)
List(forward, reverse)
.groupMap(_(0))(_(1))

def search(junction: Index, visited: BitSet, totalDist: Int): Int =
if junction == indexOf(maze.end) then totalDist else
if junction == indexOf(maze.end) then
totalDist
else
adjacent(junction).foldLeft(0):
case (longest, (nextJunct, dist)) =>
if visited(nextJunct) then longest else
Expand Down