Skip to content

The Advent of Code 2021 Day 20, Bruteforce McBruteface

Posted in Advent of Code

Hello everyone, it’s time for your Advent of Code 2021 Day 20 cruise log. Today we will go super quickly since I just wrote the Day 17 log and it is getting too late for working in front of a screen. I’m getting old now, I don’t have the blood to drown myself in litres of Redbull under piles of pizza while going for coding all-nighters. Now I’m more a man of having a tisane or camomille, at most when home alone and adventurous I’ll get a glass of wine or two. Today only water and espresso this morning before my workout. Let’s get this over with.

Today felt quite trivial but unfortunately, I didn’t quite think about taking screenshots, so it’ll be even shorter. In this challenge, we would start with a zoom and enhance algorithm with an image. These compound our puzzle input. The algorithm can get quite greedy but we’ll worry about that later.

The first step for me was reproducing the generated image from the sample which luckily we get a display of the first two steps. Most of the work was visual debugging of the first transformation. For some reason, the first half’s rendering was always messed up. Eventually, I realised it was because I would print the image using positive coordinates on an infinite plane. An infinite plane where the zoom and enhance algorithm would expand the image beyond the bounds of the original image.

Once I noticed that it was all cake. I wrote my printing code to go from the minimum coordinates to the maximum ones. Now the image renders correctly and the part one test passes. From there I run the code against the real input and it take about a second or two which feels slow but it yielded the right answer. However, that slowness tipped me to add something. A debug print to check which round I am on to figure how long it would take to enhance an image. This came in handy for the second part.

In the first part, we had to recursively run that image enhancing algorithm twice and return the number of lit squares. For part two we gotta run this fifty times which I feel will be very long. Now I’m quite glad I wrote that print, the unit test for part two takes about forty seconds to run. Not great but from the real input size, maybe this will take about half an hour to run.

I could either spend that time or even multiple hours trying to make this run faster. Or, I can just run it, come back when it’s done and enter the answer. Since I had to start work soon, I chose the latter option. As it was quite a busy morning, I went to check around lunchtime and the algorithm yielded an answer.

Now I’m kinda hoping that I didn’t run into some crazy edge case where I get the wrong answer. This would mean rewriting this to run faster and spending hours to get something useful. Fortunately, there was no need, the answer was correct and I captured my Day 20 stars. This feels good.

Thank you for reading my Advent of Code 2021 Day 20 log, I will see you when I see you. You can check out my previous entry right here.

Photo by Mahdi Bafande 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: