Skip to content

The Advent of Code 2021 Day 7 log, return of the crab

Posted in Advent of Code

Hello everyone, it’s time for your Advent of Code 2021 Day 7 log. I felt like today’s challenge would be trickier than yesterday’s. We had to deal with some fuel calculations and AoC’s fuel calculation day is always a painful one. Let me tell you the story about how I rescued crabs trying to rescue me.

Today we enjoyed an easy, chill description. We have a bunch of crabs with tiny submarines coming to rescue us for, reasons. We need to gather them in the same position and each step costs one unit of fuel. Nothing a double for-loop cannot solve. Pretty straightforward bit of code. Still, I took some time figuring how this simple solution would become a nightmare to adjust for part two. However, I chose to stop living in fear and just go for it. Within ten minutes or so dunked that. One flow, one star. Next!

The second part felt like it might drive me crazy. I didn’t even see what I had to do yet and still, I expected the worst possible outcome. Some dodgy bucketing or weird old math class I would need to solve it. Heck, I even started to change my base type from Integer to BigInteger before panic vanished.

I had one more look at the input and realised the values were too small for me to deal with BigInteger. At this point, the solution looks obvious, a factorial. I write the piece of code, it fails. It failed so hard it’s preposterous. I got a large negative value, possibly a long value that cannot fit into an integer.

That’s it, I freaking knew it! It’s another one of these nonsensical part two. I got got once more. Part one was too easy to be true and now I paid the price for my foolishness. Why would I even dare believe this was gonna be a happy ending. Back to the blackboard.

Now I’m going with baby steps, going through the problem again I have one of these moments. You know, the kind where you feel like you just awoke from a dream where you were a little retarded. Not much, but just enough to think that factorials are made from summing numbers from 1 to N. I renamed and updated my part two distance calculation method. Even though I made the necessary adjustments, I’m still off by two. While it did not surprise me since I still didn’t like the taste of failure. Back to reviewing the problem statement.

Since I might have missed something, I’m going through the problem and sample data again. My double loop would go through the initial crab positions. The correct position was that of a crab. After a bit more time pondering, I noticed something. While the correct position from which fuel would be cheapest was that of a crab for part one, it was not for part two. Maybe that was done on purpose to misdirect us. The second part cheapest position was not one of the initial crab positions.

Once I noticed that the refactoring was quite minor, instead of looping through the initial position like a dumb person, I looped from the smallest position to the highest hitting each and every one of them. Then it worked. It freaking worked! And all under 40 minutes, which is not amazing for a day 7 but still not the worst. Now that the seven days of grace ended, it’s time to relax, play games and maybe even read something since I still have two-three hours left before starting work.

Thank you for reading my Advent of Code 2021 Day 7 log, I will see you tomorrow. As usual, I will push the code to my repo for this year on Github. Feel free to check out my Day 6 log right here.

Be First to Comment

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    %d bloggers like this: