Skip to content

The Advent of Code 2021 Day 4 log, playing myself

Posted in Advent of Code

Hello everyone, it’s time for your Advent of Code 2021 Day 4 cruising log. Today I played myself. Not once, not twice, but thrice if not more. Let’s jump into how overthinking got me to waste too much time on something that should have taken me thirty minutes top.

When I saw we’ve got a bingo, I started typing code like the fastest man alive. Brain flying all around, putting this bingo model together. Fairly quickly I got to run the sample input through my code. Exceptions thrown. I missed that bingo lines may have more than a space between numbers. I fixed. Exceptions still flying around. I check the test input. I check the exception and as it turns out, I didn’t cater for the case where the line starts with a space that would yield an empty entry. From there I trim my line before the split and now it works. The test is green and the real input grants me the first star of the day.

The second part was about finding the last winning bingo board. This is where things started to go wrong. Well, not at first. It felt super easy, barely an inconvenience, about ten minutes later after starting to implement my part two I had code that passed the test for the sample input. Then, believing that I was about to clutch my second star of the day I ran the code against the real input. It failed in the most spectacular manner with exceptions and all the whistles.

After some fiddling to avoid going outside of the draw bounds, I stopped getting exceptions, instead, I kept getting a result of zero. Better but still wrong. Yet, I got to the point where I thought why not? Why would zero not be the answer? So I entered that and it was the wrong answer. There I decided to give the problem statement another read in case I missed something.

I then realised something when reading the problem statement once more. A realisation that had me dropping the hardest facepalm of this Advent of Code 2021.

My sophisticated part one solution had a bingo model relying on bingo line instances. Horizontal lines dammit. That’s the problem. All the sample winners were when horizontal lines got completed. But the problem states that vertical lines count as well. How did I miss that? I guess it was from me rushing it to enjoy more of the morning. Looks like that backfired hard.

Now, seeing the error of my ways, I decided to add some handling for vertical lines in the Bingo board class. Eventually, I got something working. The all the tests still passed but the input failed for part two. Back to square one.

At this point, I’m stumped so I take a break and go for breakfast and relax for an hour or so. I grabbed breakfast with the girlfriend, watched some Netflix then played some Smite on the Switch.

Upon returning to my desk I realise that my code is now too convoluted to make any real progress. It felt like I would save more time rewriting my solution rather than attempting further fixes. Luckily, I figured I would only need to rewrite the internals of the bingo board class. I deleted the bingo line class alongside its references and got started.

A few minutes later I got back to having all the tests passing and yet, I got the same result as previously. The big zero starring at me. Where did it come from? Does it mean that the whole board has all its numbers marked? This can’t be right. From there I started writing a bunch of random prints to see more clearly what’s going on.

This confirmed that I would only get zero because all numbers would be marked. Further print debugging got me to notice that code would always draw the final number. This could be the key as to why I don’t get the right outcome.

After another few minutes of reflection, I wrote a print function to check the state of each board at any point. That’s when it hit me like a truck, several boards can become winners in the same round so keeping only one winner for each round was bogus.

From there I updated my fudged bingo implementation to handle multiple winners each time the parent implementation would yield a winner. Now when the current round would end, I would remove all winners. The test passed again but this time this piece of code helped me clutch the final star of the day.

This reminded me of something, when unit tests aren’t enough or that the dataset is tricky, nothing beats some not-so-random prints. Nothing except for a breather and a coffee when not thinking clearly because now that I write this from my notes, I realise that I could have done the multiple winners thing before rewriting my bingo board. Definitely wasted an hour there so I better remember that for the more challenging days.

Thank you for reading my Advent of Code 2021 Day 4 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 3 log right here.

Photo by cottonbro 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: