Skip to content

The Advent of Code 2020 Day 16 log, Alive in the zone

Posted in Advent of Code

Hello, there and welcome to my Advent of Code 2020 Day 16 cruise log. I very much enjoy this sort of puzzle, quite the fun to play around and debug. Yet, it did not start in the best of ways. After a good evening playing football with the lads and scoring three goals thus helping us through a win, we went out. Not out out, reasonable, best-burger in London kind of out. Plus the place is next to the football pitch so we went straight there while respecting pandemic restrictions. Eventually got home slightly intoxicated but satisfied and looking forward to waking up early to tackle day 16.

Little did I know, things would not go according to plan. My routine for this Advent of Code is to wake up somewhere between 4 and 4.40 am so that I have time to prepare. The preparation involves creating my day package with tests ready to fill with input and output to check. This takes a few minutes for sure, but if you compete in a leaderboard one minute makes all the difference.

Yesterday, I literally lost the opportunity to catch up points on our London office #1 guy. Unless he drops in the next few days now will be tricky to catch up with him but I’m hanging in there. At least, myself doing well also helps my office rank better compared to other offices. Surprisingly Cardiff is topping the rankings with London second, New York, Lisbon and the others are trailing. We made a good gap and now need to grab the top spot.

So back to the plan disruption, you should have seen the sheer look of terror on my face when I saw my watch display 5.08 am. Eight whole minutes past the problem release time. To me, it is an unacceptable time to arise from my sleep, regardless of yesterday’s happenings. I grabbed some random clothes as I stumbled towards the office, girlfriend’s sister is staying with us for a bit and we don’t want to run into her naked at 5 am. This would raise too many questions I don’t have time to answer and holy heck it’s already 5.12 am.

Shortly thereafter, I reach the desk and turn on the small black lamp on it, turn on the laptop and turn on my brain. I don’t even remember the URL to go on the advent of code, Twitter is open I start checking the “Trending” tab and nope it’s 5.17 am so I close it after wasting a few seconds. Eventually, I open the page of today’s challenge and it’s 5.20 am, catastrophic. By the time I finish reading and mentally parsing the instructions it is almost 5.30. Some days I feel like I should stop reading the lore but I like it too much to skip. I’m doing this as much for the challenges as for the lore.

Today’s lore explains that our hero is taking a high-speed train instead of a flight for his next leg on his journey towards heavenly holidays. I’m assuming said high-speed train is the Eurostar, departing from either London (my home!), Paris (my family!), Brussels (sweet chocolates!) or Amsterdam (sweet weed!). He doesn’t understand the language and only got sparse information. The information consists of a set of rules defining valid ranges for each field on his ticket.

The first part requires us to check which tickets from other passengers are invalid from these rules. First things first, I write a test and implement the rule parsing alongside a check on whether a line is a rule. After that the first ticket line is mine and every other that follows is from another passenger. Simples! Once I got that part, I didn’t bother with other simple test and went straight for the example test. It passed. It’s almost 6 am, there is hope, I can still clutch that London office #1 spot.

Now onto the second part, from what we built in the first part we should know how to validate a ticket. Using that knowledge we need to discard all invalid tickets and deduct which field belongs to which rule. From the rule, I then figure the field name and we do some magic around “destination” prefixed names to get our result. We didn’t need to do it like that but my approach made me. I can’t help but think that there is a better way of doing this. Maybe by parsing things using other pure objects and not dictionaries.

This is where things went brown. First, my refactoring to extract the rule validation method out of the error rate calculation broke my tests. And because of the rush, I didn’t run my tests as I went. It took me longer than I care to admit to complete that refactoring. But eventually, I did it and my new part two tests pass with flying colours. However, my input didn’t. I tried a few different things, from skipping fields that don’t start with “description” doing a couple of chin-ups. Nothing.

At this point I realised, I need a ride into the zone to close this one. I need Celine to get there faster so I open Spotify and start playing the “Encore un soir” album from Celine Dion. As the music starts playing I feel it, thunder in my brain clears all thoughts, all noise. Silence save for Celine’s voice taking me for a ride. Still hearing her now as I’m writing this.

My code goes through each rule and locks the index for the first rule where for a given index all the fields match. As I read through I know there is something wrong there so I start writing more tests. Wrong tests as they all pass but eventually I find the key to unlock me. A test where a non-destination field can be matched by a destination rule. But there is a trick, that non-destination can only match that field index and no other. This means that if my destination field matches it, I will have only one locked index at the end of my loop instead of two. The test fails.

From there, it took me only a couple of minutes to right my wrongs and fix this code. I would loop until my locked indexes count matches the number of fields in a ticket. The algorithm would only lock an index if and only if it matches no other field for all tickets. The final test passed. I rush to run my input against this algorithm and it works. All under ninety minutes, it feels too long but the satisfaction capturing my final star of the day is plenty. At last, I can write this post and go for a workout. If this isn’t living, being alive, I don’t know what is.

Thank you for reading my Advent of Code 2020 Day 16 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 so feel free to check it out but not before you’ve done the challenge yourself. Bye!

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: