Advent of Code Day 1 : Kotlin TIL

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

Advent of Code Day 1 : Kotlin TIL

Day 1, all fresh and ready! No experience with the setup yet. This is the moment where I hope for the best :). But don't worry, quality should improve day after day!

First version

Note : I apologize for the quality of the first video, the following days are MUCH better :)

Full video on Youtube:

Here is a quick giphy for you to enjoy :)

Time : 29 minutes

Here is my first version of the code :

A few things to introduce you'll see in the following days :

  • I have a helper method readFileAsLinesUsingUseLines that allows me to read from the input puzzle file and load it as a list of lines. I use it every day :)
  • I always create a Kotlin class and run the code in the main method.

For this challenge, you get a list of numbers and essentially want to check them two by two (and then three by three) and find the ones that sum to 2020. I don't have much more to say :).

Final version

After spending 15 more minutes on the code, here is how my final version looks like

Even though the first challenge is not very complex, I learnt a few things :).

First, I have a habit to try to do things functionally directly. I have spent years doing for loops everywhere and do my best to be more functional these days. In this case though, the map version is more convoluted than simply looping over the array twice. But the main thing is, it also is much more efficient because I can return as soon as I found my first match! No need to duplicate the list more than necessary or do more calculations that needed.

Before I moved to the for loops though, I looked a bit deeper into the capabilities of the language, and namely the zip method of Kotlin. In my first solution, a better functional solution would probably have been to zip the same list twice (or thrice) and then do a simple mapping instead of composing maps like I would for loops.

Learnings of the day

  • Keep it simple (really)
  • Zip is nice, use it to combine lists
  • a funny one : Don't try to record your 4k screen without a dedicated GPU, because you will run in a VERY degraded environment...

See you soon for day 2!

See the repository on Github for the code :) and here for the mega thread

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