Skip to content

Commit 6d80c8d

Browse files
authored
proof-read-2 (#547)
1 parent 81101dc commit 6d80c8d

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

docs/2023/puzzles/day23.md

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Next, we need an algorithm for finding junctions that are connected to a given j
123123
<Literate>
124124

125125
```scala
126-
def connectedJunctions(pos: Point)(using maze: Maze) = List.from:
126+
def connectedJunctions(pos: Point)(using maze: Maze): List[(Point, Int)] = List.from:
127127
assert(maze.junctions.contains(pos))
128128
```
129129

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

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

177179
def longestDownhillHike(using maze: Maze): Int =
178180
def search(pos: Point, dist: Int): Int =
179-
if pos == maze.end then dist else
181+
if pos == maze.end then
182+
dist
183+
else
180184
connectedJunctions(pos).foldLeft(0):
181185
case (max, (n, d)) => max.max(search(n, dist + d))
182186

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

213217
```scala
214218
val adjacent: Map[Index, List[(Index, Int)]] =
215-
maze.junctions.toList.flatMap: p1 =>
216-
connectedJunctions(p1).flatMap: (p2, d) =>
217-
val forward = indexOf(p1) -> (indexOf(p2), d)
218-
val reverse = indexOf(p2) -> (indexOf(p1), d)
219-
List(forward, reverse)
220-
.groupMap(_._1)(_._2)
219+
maze.junctions.toList
220+
.flatMap: p1 =>
221+
connectedJunctions(p1).flatMap: (p2, d) =>
222+
val forward = indexOf(p1) -> (indexOf(p2), d)
223+
val reverse = indexOf(p2) -> (indexOf(p1), d)
224+
List(forward, reverse)
225+
.groupMap(_(0))(_(1))
221226
```
222227

223228
Finally, we perform a depth-first search that is very similar to what we used in Part 1.
224229
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.
225230

226231
```scala
227232
def search(junction: Index, visited: BitSet, totalDist: Int): Int =
228-
if junction == indexOf(maze.end) then totalDist else
233+
if junction == indexOf(maze.end) then
234+
totalDist
235+
else
229236
adjacent(junction).foldLeft(0):
230237
case (longest, (nextJunct, dist)) =>
231238
if visited(nextJunct) then longest else
@@ -307,13 +314,15 @@ case class Maze(grid: Vector[Vector[Char]]):
307314
case p if this(p) == '<' => p -> Dir.W
308315
end Maze
309316

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

315322
def search(pos: Point, facing: Dir, dist: Int): Option[(Point, Int)] =
316-
if maze.junctions.contains(pos) then Some(pos, dist) else
323+
if maze.junctions.contains(pos) then
324+
Some(pos, dist)
325+
else
317326
val adjacentSearch = for
318327
nextFacing <- LazyList(facing, facing.turnRight, facing.turnLeft)
319328
nextPos <- walk(pos, nextFacing)
@@ -330,7 +339,9 @@ end connectedJunctions
330339

331340
def longestDownhillHike(using maze: Maze): Int =
332341
def search(pos: Point, dist: Int): Int =
333-
if pos == maze.end then dist else
342+
if pos == maze.end then
343+
dist
344+
else
334345
connectedJunctions(pos).foldLeft(0):
335346
case (max, (n, d)) => max.max(search(n, dist + d))
336347

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

346357
val adjacent: Map[Index, List[(Index, Int)]] =
347-
maze.junctions.toList.flatMap: p1 =>
348-
connectedJunctions(p1).flatMap: (p2, d) =>
349-
val forward = indexOf(p1) -> (indexOf(p2), d)
350-
val reverse = indexOf(p2) -> (indexOf(p1), d)
351-
List(forward, reverse)
352-
.groupMap(_._1)(_._2)
358+
maze.junctions.toList
359+
.flatMap: p1 =>
360+
connectedJunctions(p1).flatMap: (p2, d) =>
361+
val forward = indexOf(p1) -> (indexOf(p2), d)
362+
val reverse = indexOf(p2) -> (indexOf(p1), d)
363+
List(forward, reverse)
364+
.groupMap(_(0))(_(1))
353365

354366
def search(junction: Index, visited: BitSet, totalDist: Int): Int =
355-
if junction == indexOf(maze.end) then totalDist else
367+
if junction == indexOf(maze.end) then
368+
totalDist
369+
else
356370
adjacent(junction).foldLeft(0):
357371
case (longest, (nextJunct, dist)) =>
358372
if visited(nextJunct) then longest else

0 commit comments

Comments
 (0)