This month I learned — Kotlin Advent of Code Edition

Two year ago, in November 2015 I started learning Kotlin. One of the first things I coded where the challenges from Advent of Code followed by other coding events like Google Code Jam before I started with Kotlin in client projects. For me, those coding contests are a great way to learn a new language and to improve my current skills. For Advent of Code 2017 I chose Kotlin again to get even better.
I just finished Day 15/25 and already learned a lot! Enough to write an article about the nice features of Kotlin I lately discovered after two years of actually using it. Let’s start!
Create an object for each Day
Usually, for each problem I create a new file (DayX_ProblemName.kt
) containing a main(args)
function which can be executed. I usually add a few top level functions and classes to actually solve the problem and reuse some code between part one and two. Those top level functions have to be public
or internal
to call them from my unit tests. (I want to train TDD and don’t go for a quick & dirty solution).
After solving a few problems my project got messy. I had multiple Position
classes and multiple parseInput(input: String)
functions. It gets really annoying to have a huge list of top level functions in auto completion. Until I found an interesting pattern when checking Dan Lews’ solutions:
One object
for each Day. This bundles all top level functions per day with a DayXX
prefix and prevents those methods to constantly show up in auto completion. My new starting file for each daylooks now like this:
generateSequence(seed, nextFunction)
Some problems require a brute force solution and therefore a never ending loop. In the past years I used a while(true)
loop and manually incremented a variable to find the next solution. This year I discovered generateSequence
which becomes handy whenever the next value is always based on the recent one.
Not everything is a List
Sometimes you have to remember basic data structures. Not every list of items is a List
. There are other collections which can be super useful:
Set
, doesn’t contain duplicates. By converting a List
to a Set
duplicate are automatically removed (that’s how .distinct()
works). If the length changed the list contained duplicates.
Stack
, can be used to replace recursion with an iterative loop
Destructuring Declarations and RegEx
Kotlin allows destruction of objects. This works based on Kotlins extension functions for common classes named componentX()
(1–5). Whatever an object returns for those methods can be assigned to multiple variables at once. List
for example can be destructed to the first five list elements. Data classes use the first five properties as defined. Here is an example when parsing input data with a regular expression.

Named lambdas
Multiple nested lambdas can get confusing with early returns. It is impossible for the compiler to know which lambda is meant with return@forEachIndexed
when multiple forEachIndexed
exist in the context. Better give those lambdas a meaningful name.
Kotlin Script files (.kts)
I checked others solving Advents of Code with Kotlin. Some even used a Kotlin Script (.kts
), the format you’ll all use to write your gradle files in the near future. From the docs:
Kotlin can also be used as a scripting language. A script is a Kotlin source file (.kts) with top level executable code.
Kotlin scripts are pretty cool because input files can be placed directly next to the script file. I’d love to use them to get rid of the resource and package folders and flatten my project hierarchy even further. Sadly the integration in IntelliJ is not good. Currently it’s not possible to use code from other files or libraries. Also junit tests are not supported which is a huge blocker for me.
At least it shows you can solve the problems with nothing more than pure Kotlin. Kudos!
Have fun solving Advent of Code yourself. If you’re interested here are my solutions:
Ping me on Twitter if you have any questions