Hello everyone, it’s time for your Advent of Code 2021 Day 17 cruise log. I’m late by a few days as days 16 got me good and a busy weekend led to me skipping days 18 and 19. Eventually, I managed to defeat day 16 after day 17 but only took unrefined notes for this post. Do not worry, the day 16 revenge post will arrive once I edit it. Since I completed day 20 fairly quickly this morning, I will post it as well tonight, I hope. Alright, enough ramblings, let’s jump into. Note that all the date references are from notes taken on December 17th when I took on that challenge.
As I write this, I don’t quite remember what this was about but luckily, I grabbed screenshots as I went through the challenge. Oh yes, this is the one where we need to calculate the launch vector to launch a probe with style. Here, the input data consists of some random text mixed with x and y ranges defining a target area. The target area in which we want our probe to land.
First things first, I wrote some code to make sure I understood the problem. This code uses the answer vector from the sample and counts explicitly the number of steps needed to reach the target area. In the end, I check that the position is part of the target area.
Fortunately, this proves my containing check works as expected. Now I make a change to make sure I don’t need to hardcode steps. I add a canBeReached
method that returns true if we’re before the smallest x and y values from the target area meaning that we’re not there yet but still can get there.
Now this works as well, so far I’m only going through these steps by running the debugger. Even though it is a good start, I still need to guess the right vector. After some experimenting, I figured I should search for the first vector where the final y is not below the target area. From there, I update the vector until the final x is not after the target area ends. After writing that code, I ran the test and it passed!
From there I followed my AoC tradition of running code against real input as soon as my part test passes.
Even though the test passed but I got the wrong answer with the real input. Time for a break and a shower. Unlike yesterday, at least I managed to get the sample input testing all green. That is something. But still, let’s have a break since I’ve gotta start work in about an hour.
Flash-forward a few hours, after a long yet interesting day at work ending at about 6 pm. It is finally time to switch laptops and finish this. I decided to use flair on this one and try to see what happens if I explore all the vectors with y between the 0 and the bottom-right y. The test passed and I got my first star in 48 hours!
As for the second part, I dreaded another hours long challenge but fortunately, my part one implementation did the heavy lifting. From there a double loop iterating through all possible vectors worked. 32 stars, one day too late but I’ll catch up with day 16.
Thank you for reading my Advent of Code 2021 Day 17 log, I will see you when I see you. You can check out my previous entry right here. You can find my code in my 2021 Advent of Code repository on Github.