Advent of Code Day 2 : Kotlin TIL - regex destructuring

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

Advent of Code Day 2 : Kotlin TIL  - regex destructuring

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 the mega thread for all entries.

First version

Full video on Youtube:

Here is a quick giphy for you to enjoy :)

timelapse of me solving day 2

Time : 23 minutes

Here is my first version of the code :

As usual now, quick and dirty. Many things I want to fix when I come back to it :

  • I don't want to split many times like this. There must be a cleaner way to create the data structure from the lines.
  • I don't like the multiple binary comparisons. I am implementing a XOR Gate, hopefully it is available in the standard Kotlin library.
  • I think most of the code can be embedded into the data class, responsabilities belong to the Class itself :).

Final Version

Quite a bit smaller, but also MUCH cleaner!

Learnings of the day!

Let's go through things :

  • First, I got rid of the top class and run inside a main function directly, an advice I got from Twitter.
  • I found that the XOR gate is part of the standard library! More reasons to love Kotlin every day :).
  • My main and happpiest discovery of the day though is Regexp destructuring! That's a mouthful but it's crazy cool stuff : You can basically create a regexp and use it to return all the data extracted as groups; which I can then use to create my data classes. Definitely a super cool find, and already worth writing the blog and redoing the exercises twice!
  • Last but not least, I improved my recording setup, and am now using OBS to record myself

That's it! See you tomorrow for more!  

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!