-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay23.worksheet.sc
71 lines (54 loc) · 2.21 KB
/
Day23.worksheet.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import aoc.*
import collection.immutable.BitSet
val maze = io.Source.fromResource("2023/day-23.txt").getLines.toVector
val area = Area(maze)
val slopes = Map.from[Point, Dir]:
area.pointsIterator.collect:
case p if maze(p) == '>' => p -> Dir.E
case p if maze(p) == 'v' => p -> Dir.S
case p if maze(p) == '<' => p -> Dir.W
case p if maze(p) == '^' => p -> Dir.N
val walkable = Set.from[Point]:
area.pointsIterator.filter(p => maze(p) != '#')
val start = walkable.minBy(_.y)
val end = walkable.maxBy(_.y)
val junctions: Set[Point] = walkable.filter: p =>
p.adjacent.count(walkable) > 2
.toSet + start + end
def connectedJunctions(pos: Point) = List.from[(Point, Int)]:
def walk(pos: Point, dir: Dir): Option[Point] =
val p = pos.move(dir)
Option.when(walkable(p) && slopes.get(p).forall(_ == dir))(p)
def search(pos: Point, facing: Dir, dist: Int): Option[(Point, Int)] =
if junctions.contains(pos) then Some(pos, dist) else
val next = for
nextFacing <- LazyList(facing, facing.turnRight, facing.turnLeft)
nextPos <- walk(pos, nextFacing)
yield search(nextPos, nextFacing, dist + 1)
next.headOption.flatten
for
d <- Dir.values
p <- walk(pos, d)
junction <- search(p, d, 1)
yield junction
def longestDownhillHike(pos: Point, dist: Int): Int =
if pos == end then dist else
connectedJunctions(pos).foldLeft(0):
case (max, (n, d)) => max.max(longestDownhillHike(n, dist + d))
val ans1 = longestDownhillHike(start, 0)
type Index = Int
val indexOf: Map[Point, Index] =
junctions.toList.sortBy(_.dist(start)).zipWithIndex.toMap
val fullAdj: Map[Index, List[(Index, Int)]] =
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)
def longestHike(junction: Index, visited: BitSet, totalDist: Int): Int =
if junction == indexOf(end) then totalDist else
fullAdj(junction).foldLeft(0):
case (max, (j, _)) if visited(j) => max
case (max, (j, d)) => max.max(longestHike(j, visited + j, totalDist + d))
val ans2 = longestHike(indexOf(start), BitSet.empty, 0)