8
votes
Day 12: Hill Climbing Algorithm
Today's problem description: https://adventofcode.com/2022/day/12
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>
I did this manually in Sheets. Both parts were trivial. I'm not sure there's a way to do this with formulas, to be honest. (improved image!)
This is what I split it out with
edit: those stats were wrong -- I placed 7322/6831 :)
Late posting today because I wanted to add some caching for part 2, then realised I was making a mistake and realised there was a much more elegant solution - no, not my overly verbose code - the idea is what's elegant.
Part 1
I wrote a nice library with general `bfs`, `dfs`, `dijkstra` and `A*`. You give them a start state, a neighbours function and a goal function and they spit out the shortest path (along with the path and actions taken to get there). Due to some incredibly dumb mistakes on my part, I couldn't get any of them to work.lolicopters.
So I wrote my own breadth first search, which I tend to do a lot for AoC. No need for
dijkstra
orA*
since each step of the path only adds 1 to its size/cost.For part 2 I went even dumber and ran that on all starting points ('a' in the input). I wanted to add caching so that on subsequent searches if any point during the search is in the cache the solution is already there. This is definitely over-engineered since running all searches without the cache would have been OK anyway. Now, towards the end of that process, I realised... Holy crap. If I start at the end point -'E and stop searching as soon as I find the first 'a' - that's the answer for part 2. I needed some functions to get the appropriate neighbours for the reverse path and to check whether the search was complete - i.e, have we found an 'a' height marker and... oooh myyyyy G, I had myself the same generalised
bfs
as in my library.Part 2
Part 2 then become: search from 'E' and stop searching when you hit the first 'a'. Need to make a new function that only allow neighbours that are at most 1 lower. This makes part 2 unusually faster than part 1.
Rust
Rust
Point2D