Skip to content

The Advent of Code 2021 Day 16, Revenge of the Nerd

Posted in Out-of-Season Advent of Code

Hello everyone, it’s time for your Advent of Code 2021 Day 16 revenge cruise log. Weirdly enough I figured this one out after finishing Day 17 almost a month back then life got a little crazy. Crazy in a good way that resulted in me having zero time to keep up with this Advent of Code to the point that I even forgot to publish this entry. Since we’re way past December 16th, this will start a new series which I will call Out-of-season Advent of Code. This will fit just fine among the other days which I didn’t do in 2021 and maybe I’ll do some more from the past which I hadn’t done or failed to complete previously.

Decided to rewrite more procedural code and it kinda worked, the first 5 tests passed on the first run. Way better than yesterday’s flop.

Decided to write more procedural

I feel like my brain is on fire, and start debugging the 6th test, I realise that my code is trying to read bits that don’t exist. This is where I remember that the code should ignore last zeros and maybe these throw the errors. A few ifs added here and there and the test passes.

Emboldened by today’s successful streak I decide to run all the tests at once and they all go green! Fuckin finally. Can’t believe I spent hours on that without getting half the cases passing but tonight I’m good.

The biggest difference is that yesterday I got stumped by some design problem at work and lacked sleep from waking at 4 am. Now I woke up at 7 am, smashed the day 17 AoC and solved that design issue at work. The Naggernation is back baby! Let’s get that first day 16 star:

Deciding to be cautious this time, I chose to write and validate test cases one by one, the first being the sum of subpackets:

As I keep typing, I get more and more tests passing so I decide to write all test cases and run them all at once even though I know I didn’t implement all cases yet.

Looks like the hardest part really was the first one. I have a funny feeling that this will be this year’s IntCode but at least my code is clean enough to handle the next days. And I as wrote this and got all my tests green, the real input run threw an exception:

Checking that value online I realise it is too big to be parsed into an integer, time to pull out the big integer. It’ll take some time to adjust my code but should be straightforward since I don’t parse the data until it’s time to compute values.

Looks like it pays off, the tests are still passing:

All the tests still pass

Unfortunately, it is the wrong answer. Not sure what to do next so I go back to the problem and it jumps to my face. The sum and product operations have more than two packets. More precisely, the operations with exactly two subpackets are specified and sum and products are not. I realised my mistake and went back to coding. I switched my tacky two packets only sum and products with elegant streams reductions.

And the tests still pass.

The answer felt more promising but still failed.

I must have missed something else, something similar. Nope nothing, maybe I need an even bigger number, like a BigDecimal. Tests still pass, let’s try the real thing. And it’s the same. I decided to try the examples, maybe I missed a case.

I started adding examples from the first part and all tests passed. Not sure what to do next. Just to try, I decided to remove the defensive code to get out of processing when there are only zeros. It could reveal something.

Unfortunately, careful debugging revealed that the exit code was necessary and returns the right packets. At this point, I decided to call it a day and to have another look once I’m done with day 18. I do need to rest if I want to be sharp for the next challenge.

00:56 about to head to bed after some gaming and I get inspired. It was more of a feeling, I felt like something could be wrong with by packet reading on the case where we read bits with a limit as to how many we read. So I decided to add a print to check how many times I would end up with a negative amount of bytes to read after reading in such way. It should never go below zero. And it didn’t, for the unit tests:

Then I ran the code for the real input and oh boy was there more negative stuff there than in the classic Twitter timeline.

It expends further than that

So now instead of reading my children while checking the value after the reading, I decided to have another linked list where I would copy exactly the number of bytes to read for the children. This must be it, that would explain why my result is too low, some blocks get absorbed by others and somehow this still yielded the right result for the first part.

Now the negative stuff is gone

Now that I finally got rid of the negative numbers, the value looks way bigger, let’s try my luck.

And back on track, only four hours before the next challenge drops! Hillarious, considering I’m publishing this a month later. Second edit: I forgot to publish in January so now it’s been over 3 months. This makes it a very much out-of-season Advent of Code posting.

Thank you for reading my Advent of Code 2021 Day 16 log, I will see you tomorrow (3 months ago). All the code is present on Github. Feel free to check out my Day 15 log right here.

Photo by Matej from Pexels

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: