Hello, there and welcome to my Advent of Code 2020 Day 24 cruise log. Today was a good day, I woke up at about 4.30 am but chose to stay in bed a little longer. Obviously, I didn’t make the blatant mistake of closing my eyes. Instead, I did some breathing exercises trying to build up my focus for the day. I like to believe that it did work to some extent. Today’s challenge ended well but it could have gone sideways like yesterday. Let’s start with the lore and then get into it.
On this blessed day, our hero finally reaches his hotel. However, the floor stands between him and his room. As it turns out, he cannot get to his room before the contractors finish putting the tiles. Those tiles are two-faced just like some people can be, with a black and a white side. Hopefully, nobody tries to cancel Eric on that, we need more Advent of Code and people like him around, not less. Oh yes, the tiles, by default the tiles show their white faces. So far so good, then we get instructions (our puzzle input) to flip them onto their black faces. We want to know how many black faces are visible when we’re done. The complexity lies in the fact that the tiles have a hexagonal shape. Let’s jump into it.
The instructions give us directions to reach a certain tile from the central tile so that we flip it. Since we’re dealing with hexagons, we can travel into six directions. Count all the cardinal ones with cross-overs except north and south then you get them. This is where my dumb face decides to use pointers again. I went for a structure that contains a map of directions connecting to tiles pointers. It did sound good when I came up with it. I even wrote steps and tests on paper first before writing any code. As I found out later, it wasn’t enough.
Surprisingly, the first bit of code I wrote was fine. I wrote a direction alias on int with all the directions we can use. I put together my directions parser fairly quickly alongside appropriate tests. Then things got messy. My code would create a new tile for every instruction if it didn’t exist and travel to it. It worked for the first few tests. However, it wouldn’t last long. As soon as I add the tests to check you can go away a few tiles and return to the central circularly, the green of my tests skedaddles away. After some trickery involving linking back tiles recursively with some magic limits, it came back.
From there, confident that I found the key to my first star today, I run my part one test. It fails. Even better, it returns twice the result that I expect, some links are definitely missing. I put all the magic constants and ifs that I could but no conclusive results. Outside that the wrong answer varies here and there.
Since it’s been two hours already and I can take a hint, I figure I must be going in the wrong direction. There has to be some sort of structure I can use where I can link to an element without knowing it’s there. The only one I can think of is some grid. But I know nothing such as hexagonal grids, I saw some in old games but that’s it. Maybe it’s time to head back to the problem’s subject and see if I failed to parse any key detail. That’s when I see it. The obvious, laughing at me.
On my first read, I glossed over the mention of a hexagonal grid. It has a Wikipedia link, I click on it. After eye-brushing the page for a few seconds I close it, then reopen it only to close it again. It felt like a clue my brain didn’t want to pick. But it gave me an idea, it’s DuckDuckGo time! I look for “hexagonal grid data structure” and find a StackOverflow link that seems right for me. I skip through straight to the answer from a paid nerd. That’s not me being rude, it’s actually his username check it out. Bullseye. He mentions a blog post from Amit Patel on hexagonal grids and there is a section on how to represent them as coordinates. Like coordinates relative to a centre. Remember the central tile? That will be my point of origin.
Armed with that new cool weapon, I still need to understand how to wield it. The page has enough for me to figure it out. Now I need to figure directions and how to move, the page has enough for me to put together a new structure. Coordinates with positions on axis x, y and z. Alongside that, I create a method to derive my direction using a vector also of type coordinates. About half-an-hour later I finish updating my tests to use the new structure and delete the old one. Some tests don’t pass. I question myself once more, it’s about 8 am and my girlfriend woke up. Time for me to have a break and for us to have breakfast.
After a fair hour of eating followed by some relaxation, I came back to my desk. The bug was still there and since I knew from my previous tests that my instructions parsing is on point, this has to be from my direction vectors method. I go through the paper where I noted the directions and vectors. At this moment I can see there is something wrong. If I sum all the vectors, I should have a null vector with x, y and z set to zero. But there was a difference. After careful examination of my notes, I see that my south-east vector is the same as my south-west. I correct that copy-paste mistake and run my tests once more, they still go red.
Eventually, I return to the blog post from Amit and decide to recheck my notes; maybe I rushed the notations of my vectors. And it is exactly what I did, fast-forward five minutes, I realise that my south-west vector is wrong. My eyes must go very bugging when it sees the letters S and W together even when the J is missing. As it turns out, my y and z values are swapped in my south-west vector. I go ahead and fix it and at last, my tests pass and I can finally claim my first star of the day.
As for the second part, we’re going full Conway’s game again with some rules to flip the tiles so it’s basically day 17 part two all over again. Obviously on an infinite plan, again. So I did the rational thing of copying then pasting the code from that day. I only need to replace my cubes from then with the coordinate objects and the mapped state values with colours and voila.
Once I updated the state changing rules I was good to go so I added the part two test straight away. And boom, green tests for everyone and Bob’s your uncle! I run the code against my input and collect the 48th star of this Advent of Code. To me, it will be a success no matter what happens tomorrow. Already got eight more stars than I ever did on any edition, the pandemic helped a bit but completing most days in the early morning was a big plus. I’ll try to keep that waking early habit even after the Advent of Code completes because it’s a good habit to keep.
Thank you for reading my Advent of Code 2020 Day 24 cruise log, I will see you tomorrow, maybe. In the meantime, you can check out my Go Cloud series about building and deploying a Golang app to AWS. Also, I will push my code on Github at CodingNagger/advent-of-code-2020. Feel free to check it out but not before you’ve done the challenge yourself. Bye!