You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/2023/puzzles/day17.md
+266Lines changed: 266 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,278 @@ import Solver from "../../../../../website/src/components/Solver.js"
2
2
3
3
# Day 17: Clumsy Crucible
4
4
5
+
by [@stewSquared](https://github.com/stewSquared)
6
+
5
7
## Puzzle description
6
8
7
9
https://adventofcode.com/2023/day/17
8
10
11
+
## Solution Summary
12
+
13
+
This is a classic search problem with an interesting restriction on state transformations.
14
+
15
+
We will solve this using Djikstra's Algorithm to find a path through the grid, using the heat loss of each position as our node weights. However, the states in our priority queue will need to include more than just position and accumulated heat loss, since the streak of forward movements in a given direction affects which positions are accessible from a given state.
16
+
17
+
Since the restrictions on state transformations differ in part 1 and part 2, we'll model them separately from the base state transformations.
18
+
19
+
### Framework
20
+
21
+
First, for convenience, let's introduce classes for presenting position and direction:
And now a few convenience methods that need the input:
61
+
62
+
```scala
63
+
valxRange= grid.head.indices
64
+
valyRange= grid.indices
65
+
66
+
definBounds(p: Point) =
67
+
xRange.contains(p.x) && yRange.contains(p.y)
68
+
69
+
defheatLoss(p: Point) =
70
+
if inBounds(p) then grid(p.y)(p.x) else0
71
+
```
72
+
73
+
### Search State
74
+
75
+
Now we want to be able to model our state as we're searching. The state will track our position. To know what transitions are possible, we need to keep track of our streak of movements in a given direction. We'll also keep track of the heat lost while getting to a state.
76
+
77
+
```scala
78
+
caseclassState(pos: Point, dir: Dir, streak: Int):
79
+
```
80
+
81
+
Next let's define some methods for transitioning to new states. We know that we can chose to move forward, turn left, or turn right. For now, we won't consider the restrictions from Part 1 or Part 2 on whether or not you can move forward:
82
+
83
+
```scala
84
+
// inside case class State:
85
+
defstraight:State=
86
+
State(pos.move(dir), dir, streak +1)
87
+
88
+
defturnLeft:State=
89
+
valnewDir= dir.turnLeft
90
+
State(pos.move(newDir), newDir, 1)
91
+
92
+
defturnRight:State=
93
+
valnewDir= dir.turnRight
94
+
State(pos.move(newDir), newDir, 1)
95
+
```
96
+
97
+
Note that the streak resets to one when we turn right or turn left, since we also move the position forward in that new direction.
98
+
99
+
### Djikstra's Algorithm
100
+
101
+
Finally, let's lay the groundwork for an implementation of Djikstra's algorithm.
102
+
103
+
Since our valid state transformations vary between part 1 and part 2, let's parameterize our search method by a function:
104
+
105
+
```scala
106
+
defsearch(next: State=>List[State]):Int
107
+
```
108
+
109
+
The algorithm uses Map to track the minimum total heat loss for each state, and a Priority Queue prioritizing by this heatloss to choose the next state to visit:
110
+
111
+
```scala
112
+
// inside def search:
113
+
importcollection.mutable.{PriorityQueue, Map}
114
+
115
+
valminHeatLoss=Map.empty[State, Int]
116
+
117
+
givenOrdering[State] =Ordering.by(minHeatLoss)
118
+
valpq=PriorityQueue.empty[State].reverse
119
+
120
+
varvisiting=State(Point(0, 0), Dir.E, 0)
121
+
val minHeatLoss(visiting) =0
122
+
```
123
+
124
+
As we generate new states to add to the priority Queue, we need to make sure not to add suboptimal states. The first time we visit any state, it will be with a minimum possible cost, because we're visiting this new state from an adjacent minimum heatloss state in our priority queue.
125
+
So any state we've already visited will be discarded. This is what our loop will look like:
Notice how `minHeatLoss`` is always updated to the minimum of the state we're visiting from plus the incremental heatloss of the new state we're adding to the queue.
141
+
142
+
### Part 1
143
+
144
+
Now we need to model our state transformation restrictions for Part 1. We can typically move straight, left, and right, but we need to make sure our streak straight streak never exceeds 3:
145
+
146
+
```scala
147
+
// Inside case class State:
148
+
defnextStates:List[State] =
149
+
List(straight, turnLeft, turnRight).filter: s =>
150
+
inBounds(s.pos) && s.streak <=3
151
+
```
152
+
153
+
This will only ever filter out the forward movement, since moving to the left or right resets the streak to 1.
154
+
155
+
### Part 2
156
+
157
+
Part 2 is similar, but our streak limit increases to 10.
158
+
Furthermore, while the streak is less than four, only a forward movement is possible:
159
+
160
+
```scala
161
+
// inside case class State:
162
+
defnextStates2:List[State] =
163
+
if streak <4thenList(straight)
164
+
elseList(straight, turnLeft, turnRight).filter: s =>
165
+
inBounds(s.pos) && s.streak <=10
166
+
```
167
+
168
+
## Final Code
169
+
170
+
```scala
171
+
importlocations.Directory.currentDir
172
+
importinputs.Input.loadFileSync
173
+
174
+
@main defpart1:Unit=
175
+
println(s"The solution is ${search(_.nextStates)}")
176
+
177
+
@main defpart2:Unit=
178
+
println(s"The solution is ${search(_.nextStates2)}")
-[Solution](https://github.com/stewSquared/advent-of-code/blob/master/src/main/scala/2021/Day17.worksheet.sc) by [stewSquared](https://github.com/stewSquared)
11
277
-[Solution](https://github.com/merlinorg/aoc2023/blob/main/src/main/scala/Day17.scala) by [merlin](https://github.com/merlinorg/)
12
278
-[Solution](https://github.com/xRuiAlves/advent-of-code-2023/blob/main/Day17.scala) by [Rui Alves](https://github.com/xRuiAlves/)
0 commit comments