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
with this graph structure in hand, we can forget about the grid
81
+
and solve the problem at a higher level of abstraction.
82
+
83
+
### Part 1
84
+
85
+
Part 1 is actually more difficult than part 2, in my opinion. In
86
+
fact, in my first attempt to solve part 1, I accidentally solved part
87
+
2! Once I saw part 2, I had to go back and reconstruct what I had done
88
+
earlier.
89
+
90
+
From a given trailhead, the same summit may be reachable by multiple
91
+
routes. Therefore, we can't just count routes; we must remember what
92
+
the destinations are. Hence, the type of the recursive method is
93
+
`Set[Pos]` -- the set of summits that are reachable from the current
94
+
position.
95
+
96
+
```scala
97
+
defsolve1(topo: Topo):Int=
98
+
valgraph= computeGraph(topo)
99
+
defreachableSummits(pos: Pos):Set[Pos] =
100
+
if topo(pos) ==9
101
+
thenSet(pos)
102
+
else graph(pos).flatMap(reachableSummits)
103
+
topo.positions
104
+
.filter(pos => topo(pos) ==0)
105
+
.map(pos => reachableSummits(pos).size)
106
+
.sum
107
+
108
+
defpart1(name: String):Int=
109
+
solve1(getInput(name))
110
+
```
111
+
112
+
As mentioned earlier, note that we don't bother memoizing. That means
113
+
we're doing some redundant computation (when paths branch and then
114
+
rejoin), but the code runs plenty fast anyway on the size of input
115
+
that we have.
116
+
117
+
### Part 2
118
+
119
+
The code for part 2 is nearly identical. We no longer need to de-duplicate
120
+
routes that have the same destination, so it's now sufficient for the recursion
121
+
to return `Int`.
122
+
123
+
It would certainly be possible to refactor this to share more code
124
+
with part 1, but I've chosen to leave it this way.
125
+
126
+
```scala
127
+
defsolve2(topo: Topo):Int=
128
+
valgraph= computeGraph(topo)
129
+
defroutes(pos: Pos):Int=
130
+
if topo(pos) ==9
131
+
then1
132
+
else graph(pos).toSeq.map(routes).sum
133
+
topo.positions
134
+
.filter(pos => topo(pos) ==0)
135
+
.map(routes)
136
+
.sum
137
+
138
+
defpart2(name: String):Int=
139
+
solve2(getInput(name))
140
+
```
141
+
142
+
One tricky bit here is the necessity to include `toSeq` when
143
+
recursing. That's because we have a `Set[Pos]`, but if we `.map(...)`
144
+
on a `Set`, the result will also be a `Set`. But we don't want to
145
+
throw away duplicate counts.
146
+
9
147
## Solutions from the community
148
+
10
149
-[Solution](https://github.com/nikiforo/aoc24/blob/main/src/main/scala/io/github/nikiforo/aoc24/D10T2.scala) by [Artem Nikiforov](https://github.com/nikiforo)
11
150
-[Solution](https://github.com/spamegg1/aoc/blob/master/2024/10/10.worksheet.sc#L166) by [Spamegg](https://github.com/spamegg1)
12
151
-[Solution](https://github.com/samuelchassot/AdventCode_2024/blob/8cc89587c8558c7f55e2e0a3d6868290f0c5a739/10/Day10.scala) by [Samuel Chassot](https://github.com/samuelchassot)
13
-
-[Solution](https://github.com/rmarbeck/advent2024/blob/main/day10/src/main/scala/Solution.scala) by [Raphaël Marbeck](https://github.com/rmarbeck)
152
+
-[Solution](https://github.com/rmarbeck/advent2024/blob/main/day10/src/main/scala/Solution.scala) by [Raphaël Marbeck](https://github.com/rmarbeck)
14
153
-[Solution](https://github.com/nichobi/advent-of-code-2024/blob/main/10/solution.scala) by [nichobi](https://github.com/nichobi)
15
154
-[Solution](https://github.com/rolandtritsch/scala3-aoc-2024/blob/trunk/src/aoc2024/Day10.scala) by [Roland Tritsch](https://github.com/rolandtritsch)
16
155
-[Solution](https://github.com/makingthematrix/AdventOfCode2024/blob/main/src/main/scala/io/github/makingthematrix/AdventofCode2024/DayTen.scala) by [Maciej Gorywoda](https://github.com/makingthematrix)
0 commit comments