Advent of Code Day 5 : Binary representations and data classes val inits

How I solved Advent of Code day 5, live, and what I learned from it.

Advent of Code Day 5 : Binary representations and data classes val inits

As usual, my first quick and dirty attempt at the challenge, followed by a (hopefully) better version with a list of things I learned during the day :)!

See the repository on Github for the code :) and here for all entries.

First version

Full video on Youtube:

Note that I ran into a lot of issues with my IDE for this day and had to reboot the computer. I even curse a little bit halfway through the video :). I decided to not edit it, after all it's been part of my experience. I do realize it's probably not the best day to watch though :)!

Here is a quick giphy for you to enjoy :)

Time : 53 minutes

Here is my first version of the code :

Final Version

Much, much shorter! What's interesting about this one is that when I came back to it I needed just about 10 minutes to create the better version. It was logical when I came back to it. Maybe proof that recording myself and having to speak has an effect on my coding :) (damn you whiteboard interviews).

Learnings of the day!

There are not many today, just two neat tricks I'm happy to have used

  • The first one is a minor, but nice one : You can initiate values in a data class on creation. I used this for seatId and it proves very useful when sorting and comparing later on. Much better than in the first version where I would have to use a function call instead and rely on a Pair in part 2.
  • The trick that makes everything much easier here is to think of the inputs as the binary representations of the rows and columns. For example, BFFFBBF would become 1000110 which is 70. The toIntfunction of Kotlin with Radix comes handy here. Coming from an electronics background, I realized that during the recording but I didn't want to risk it on camera and have to solve bugs. I think I should have tried.
  • We leverage the learnings from previous days and make use of a regular expression to quickly replace the F, L, B and R without having to repeat ourselves.
  • We also learnt about zipWithNext in the previous days, which as very handy to check for contiguous ids of seats. Once we found a break in the chain, we found our seat!

That's it! See you soon for more! Those articles take me quite some time so I'm a bit late with the competition now :)

See the repository on Github for the code :) and the mega thread for all entries.

Do you have remarks? I'm curious, let me know on Twitter!