Hi folks,
Just thought I'd share. I decided to start prepping for Advent of Code early this year and have gone back to the very first AoC to get some practice. As recent years, I'm doing my solutions in Kotlin.
I'm going to skip ahead to
Day 14 here. I was pleasantly surprised to solve Part 1 fairly quickly. After parsing the input and coming up with a formula to calculate the distance traveled by each reindeer, it was just a matter of getting the maximum of all those distances traveled. A one-liner, basically. Ironically, I found out in Part 2 that the formula I had used for the distance calculation was not quite right, even though it had somehow earned me a gold star for a correct answer.
Part 2, where we had to award points to reindeer for every second during the race that they had the lead, was a little trickier. I got the correct answer to my puzzle input with a very imperative-style solution. I guess I haven't quite made the transition to an FP mindset yet. I did have a sense that the imperative-style implementation could be refactored to a more functional style though. After a long, good run at refactoring,
I started with
this (the code that earned my 28th gold star) and slowly refactored it to this:
I have to say, I'm quite happy with how it went. The story for Part 2 is composed and layered nicely from high-level abstract, down to more and more detail. Also, I like that the extracted functions are singular in purpose and are easy to understand. At least, that's what I think when I imagine seeing the code for the first time. I'm curious about what others think though. Appreciate any thoughts/reactions.
I also created a branch to keep a history of my refactoring steps:
https://github.com/jlacar/aoc2015/blob/refactor-day14-pt2-to-functional/src/main/kotlin/lacar/junilu/Day14.kt