Advent of Code Day 3 : Kotlin TIL - drop, filterIndexed
How I solved Advent of Code day 3, live, and what I learned from it.
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 :)
Time : 37 minutes
Here is my first version of the code :
As usual now, quick and dirty, though this one turned out quite nice if you ask me. Many things I want to fix when I come back to it :
- I see a lot of repetition, that doesn't make me super happy
- I wonder if there is an easy way to have processLine work for double lines instead of using a specific runner for it?
Final Version
Quite a bit smaller, but also MUCH cleaner just like yesterday!
Learnings of the day!
Let's go through things :
- Habits die hard. I am using to just use
sublist
to take only a part of a list, but Kotlin hasdrop
andtake
as well which make things much cleaner - I found
filterIndexed
very useful to quickly drop half of the lines, instead of having to make a more complexfold
function that will only be valid for one use case. - I am still not completely happy to be using a
Pair
inside my fold function. But I have found no other simple way to keep track of both the total amount of trees found as well as the current index we want to be in. - I'm pretty happy to be using the modulo (%) function to target the right place of the line insead of repeating my list. Pretty neat
This blog had me pondering whether I wanted to drop the best solution to the problem here, or my best solution. I've decided to stop working at the moment where I've learned something new and am not ashamed of the final code :). Food for thought for the coming days.
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!