Advent of Code 2024, Day 10
Finally a problem where I accidentally solve part 2 during part 1 and get a wrong answer as a result. Makes for a quick part 2 though!
I had a single logic error in part 1, but otherwise my main issue was figuring out how to deal with recursive functions.
Spoilers
Part 1
Practically all years had a variation of this: given a map and some starting points, walk according to some adjacency rules, and score on some other rules.
This one was mostly hard because of the recursive function I had to figure out how to make work. Ultimately it was a single method that called itself. I also had to figure out some mutability and borrowing rules, but I got there in the end. The way the function works is:
- takes in the current coordinate we’re on and a Vec of Coordinates (where that particular path has been)
- adds the current coordinate to the Vec of Coordinates
- grabs its value, or 10 if it’s not in the map
- checks the value
- if it’s 9 exactly, returns the single
Vec<Coordinate>
in avec![]
, because we have a path to a summit! - if it’s more than 9, returns an empty vec, discarding the path so far, because we’ve gone out of map
- if it’s less than 9, it continues with the rest of the code
- if it’s 9 exactly, returns the single
- creates a new
Vec<Vec<Coordinate>>
- generates the coordinates of the neighbours of the current coordinate
- for each of the neighbours, it checks their value, and if they’re one more than the current value, then
- passes the coordinate of that neighbour, plus the current path so far to the function, and for each of the
Vec<Coordinate>
that the method returns, adds them the list - returns the list
It sounds kinda complicated, but basically when the path finding reaches a node where it could go multiple ways, it goes multiple ways, and records all of them.
Parsing the input to the map and keeping track of Coordinates is now easy, so I don’t mention them specifically.
The one logic error I had was that I was counting all the separate ways we can reach all the summits from all the trail heads rather than only counting the number of summits we can reach from each trailhead. For example in this situation:
0123456111
9999567891
The correct score is 1, because from that one trailhead in the top left corner I can only reach the one summit near the bottom right corner.
Even though I can take 3 different paths:
01234.....
....56789.
012345....
.....6789.
0123456...
......789.
On to part 2!
Part 2
Okay, this is hilarious. So the way I write these blog posts is when I finish part 1, I start a new blog post, write part 1, add the images, add the part 2 heading, and then go and read what the part 2 exercise actually is.
Turns out my logic error for part 1 is ... uh, the solution to part 2. This should be quick, essentially I just need to call the other method.
Brb, calling functions... ⏳
Yup. All I did was write some setup code, called the function, and got the right answers. I didn’t even read the entire part 2 description, saw that the solution to the example was 81, and I knew.
Well this was an easy part 2 😅. Another ⭐️⭐️!
Spoiler photo by Kirill Prikhodko on Unsplash