Hello there. Finally, for the first time in a few days, I woke up at 4.50 am again. That was quite a good sleep. Now it’s 6.48 am, so let’s write this, shall we? Welcome to my Advent of Code 2022 day 12 log.
I would attribute the good sleep I had to a few things. But if I had to pick one, I would say that my fiancee and I had the pleasant surprise of a white London before bed. After watching the 2021 Suicide Squad movie, we took the dog for a walk. Imagine our surprise when we saw that the town turned white. Snow layers reached up to ten centimetres deep. Our puppy never saw snow before as he’s just turned a year old.
Back to this morning, I have two words to describe it: shortest path. I knew this day would come, there is always at least one of these each year. Last year I used the Dijkstra algorithm which I learned during my first year at school. This year I couldn’t be bothered to rewrite it from scratch, so I simply copied the code from my 2021 day 15.
Obviously, that code needed some adjustments as the rules for moving from one location to the next are different from those of last year. We deal with elevation levels that determine whether we can move to a neighbour’s location or not. But this jumpstarted things.
Once I cleaned up the old code and parsed my input, I my some changes to take into consideration the elevation-based rules and voila! Or so I thought. While the test against the sample input passed, the real input execution gave me the wrong answer.
I knew my Dijkstra was perfect, I used it last year to solve a similar puzzle. And yet, it simply does not work. Working my way through the code, I spotted a few potential bugs and cleaned them up. My test kept passing but the real input result kept returning the same damn answer.
I read the problem statement over and over. Over and over, I see that I followed the instructions to the tee. This goes on for a good hour or so. Now one thing feels almost certain, I must have a bug in how I handle my input.
I write code to print the distances across the map and the sample distances look good. Next, I try to figure out how I might have messed up around the target location. Nothing.
But then I see it. The issue is my starting point. In the example, the starting point is (0, 0) which is the natural go-to when writing Dijkstra or any path-related algorithm. However, in the real input, it’s a completely different point. At this point, hope returns, I need to write code to determine the starting point and run the algorithm from there.
And it works! I finally collect the first star of the day.
Now onto the second part, it looks trickier but after reading it a few times, I realise that it’s easy. As it turns out, I already wrote my code in such a way that a minor adjustment suffices to solve this one. After writing the change, I run it against the test sample and it works. Next, onto the real thing, it works as well. I acquired star number two within an extra ten minutes. Now it’s 7.05 as I write this line. Plenty of time left to exercise and relax before starting work.
Thank you for reading my Advent of Code 2022 Day 12 log, I will see you tomorrow. If you want to check out my previous entry, you can do so here. You can even read my entries from last year there. As usual, I will push the code to my repo for this year on Github. Also, if you want to test yourself against my AoC 2022 run, I’ve created a private leaderboard. The invite code is
382952-d065ee7a . Join, if you dare.
Cover by Pixabay