8
votes
Day 10: Hoof It
Today's problem description: https://adventofcode.com/2024/day/10
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Managed to keep things simple enough for today, including reusing my code from Part 1 for Part 2. I'm finding a bunch of tools that I wished I'd found previous days like
flatCollect:
anddigitValue
.Smalltalk Solution
I was satisfied that for part 2 all I had to do was comment out one line of my code from part 1 that was checking to see if I had already encountered a location in the grid. I actually accidentally got the output of part 2 in my first iteration of solving part 1.
Part 1 & 2
From what I've read on Reddit and the other AoC community I talk to, it sounds like everyone did that. A pretty simple puzzle, especially if you did the one from last year that this one references. If I hadn't run the example input first, I probably would have accidentally gotten the Part 2 answer before I got Part 1.
Same! In trying to solve part 1, I actually solved part 2! Lol. Code here.
I happened to solve part 1 on this in such a way that made part 2 extremely easy - in fact, at first when trying to solve part 1 I was accidentally getting the answer for part 2 instead! I had to add extra code to make part 1 work, so for part 2 all I had to do was what I was already doing before.
Solution (Jai)
TypeScript
Like others have said, I also was able to satisfy Part 2 just by circumventing the uniqueness check in Part 1. I feel like this puzzle might have worked better if the parts were reversed, as the continuation should add more constraints, not remove them.
Spoilers
I went with another recursive function for this one. Seemed like a good way to stretch myself a bit! Also similar to some previous puzzles this year, I set up a
directions
array containing basic walk functions for each cardinal direction.My solution searches the map for possible trailheads (
0
) and for each one it kicks off a recursive function that does the following:directions
and calls each walk function to get neighbor coords. For each of those, it validates that they are both in bounds and that the corresponding elevation is exactly one higher than that of the current coords.9
). If so, it pushes the current coordinates to the array of known ends. (In Part 1, a uniqueness check flag istrue
to ensure the coords are only pushed if they're not already in the array.) If the current position is not a trail end, the function recurses.Parts 1 and 2 (TypeScript)
I love pathfinding puzzles!
Part 1 (Rust)
Part 2 (Rust)
Helpers
Benchmark
I'd been avoiding this one because it seemed like it would be a cumbersome pathfinding problem, but it turned out to be... very easy?
The only difference between part 1 and part 2 was whether to keep duplicates in my list of in-progress paths. Annoyingly, the
for
special form (aka comprehensions) has a weird limitation where its:uniq
option only accepts boolean literals, so I had to conditionally filter to unique values outside of the comprehension.Both parts (Elixir)
Benchmarks
Honestly, I kind of wish I hadn't used the
graph
library for Racket lol. I went back after I did part 2 and myday10/run-part01-better
procedure drops the run time for part 1 from ~17 seconds to 1 second lol. I guess that's partly just because my "better" procedure doesn't find the shortest path, just a path.Anyways, I really enjoyed that one. I did UIL Computer Science competitions in high school that had lots of graph problems like this and they were by far my favorite. If I were doing this back then I probably could've finished it in Java (without any graph library, just by coordinates) in like 5 minutes lol.
Racket
Python: Step-by-step explanation | full code
Because today was simpler, we had the chance to focus on a clean DFS implementation. It also may take the cake for "least changes between parts 1 and 2", which is always a nice break. Ultimately, I could put everything in a function and call it twice for each
0
position, summing the totals independently.This definitely feels like the calm before the storm!