Advent of Code puzzles in idiomatic Kotlin
Advent of Code is an annual December event, where holiday-themed puzzles are published every day from December 1 to December 25. With the permission of Eric Wastl, creator of Advent of Code, we'll show how to solve these puzzles using the idiomatic Kotlin style:
Get ready for Advent of Code
We'll take you through the basic tips on how to get up and running with solving Advent of Code challenges with Kotlin:
Use this GitHub template to create projects
Check out the welcome video by Kotlin Developer Advocate, Sebastian Aigner:
Advent of Code 2022
Day 1: Calorie counting
Learn about the Kotlin Advent of Code template and convenience functions for working with strings and collections in Kotlin, such as maxOf()
and sumOf()
. See how extension functions can help you structure your solution in a nice manner.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 2: Rock paper scissors
Understand operations on the Char
type in Kotlin, see how the Pair
type and the to
constructor work well with pattern matching. Understand how to order your own objects using the compareTo()
function.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 3: Rucksack reorganization
Learn how the kotlinx.benchmark library helps you understand the performance characteristics of your code. See how set operations like intersect
can help you select overlapping data, and see performance comparisons between different implementations of the same solution.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 4: Camp cleanup
See how infix
and operator
functions can make your code more expressive, and how extension functions for the String
and IntRange
types make it easy to parse input.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 5: Supply stacks
Learn about constructing more complex objects with factory functions, how to use regular expressions, and the double-ended ArrayDeque
type.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 6: Tuning trouble
See more in-depth performance investigations with the kotlinx.benchmark library, comparing the characteristics of 16 different variations of the same solution.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 7: No space left on device
Learn how to model tree structures, and see a demo of generating Kotlin code programmatically.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 8: Treetop tree house
See the sequence
builder in action, and how far a first draft of a program and an idiomatic Kotlin solution can differ (with special guest Roman Elizarov!).
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 9: Rope bridge
See the run
function, labeled returns, and convenient standard library functions like coerceIn
, or zipWithNext
. See how you can construct lists of given sizes using the List
and MutableList
constructors, and get a peek at a Kotlin-based visualization of the problem statement.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 10: Cathode-ray tube
Learn how ranges and the in
operator make checking ranges natural, how function parameters can be turned into receivers, and a brief exploration of the tailrec
modifier.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 11: Monkey in the middle
See how you can move from mutable, imperative code to a more functional approach that makes use of immutable and read-only data structures. Learn about context receivers and how our guest built his own visualization library just for Advent of Code.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Day 12: Hill Climbing algorithm
Use queues, ArrayDeque
, function references, and the tailrec
modifier to solve path finding problems with Kotlin.
Read the puzzle description on Advent of Code
Check out the solution in the video:
Advent of Code 2021
Day 1: Sonar sweep
Apply windowed and count functions to work with pairs and triplets of integers.
Read the puzzle description on Advent of Code
Check out the solution from Anton Arhipov on the Kotlin Blog or watch the video:
Day 2: Dive!
Learn about destructuring declarations and the when
expression.
Read the puzzle description on Advent of Code
Check out the solution from Pasha Finkelshteyn on GitHub or watch the video:
Day 3: Binary diagnostic
Explore different ways to work with binary numbers.
Read the puzzle description on Advent of Code
Check out the solution from Sebastian Aigner on Kotlin Blog or watch the video:
Day 4: Giant squid
Learn how to parse the input and introduce some domain classes for more convenient processing.
Read the puzzle description on Advent of Code
Check out the solution from Anton Arhipov on the GitHub or watch the video:
Advent of Code 2020
Day 1: Report repair
Explore input handling, iterating over a list, different ways of building a map, and using the let
function to simplify your code.
Read the puzzle description on Advent of Code
Check out the solution from Svetlana Isakova on the Kotlin Blog or watch the video:
Day 2: Password philosophy
Explore string utility functions, regular expressions, operations on collections, and how the let
function can be helpful to transform your expressions.
Read the puzzle description on Advent of Code
Check out the solution from Svetlana Isakova on the Kotlin Blog or watch the video:
Day 3: Toboggan trajectory
Compare imperative and more functional code styles, work with pairs and the reduce()
function, edit code in the column selection mode, and fix integer overflows.
Read the puzzle description on Advent of Code
Check out the solution from Mikhail Dvorkin on GitHub or watch the video:
Day 4: Passport processing
Apply the when
expression and explore different ways of how to validate the input: utility functions, working with ranges, checking set membership, and matching a particular regular expression.
Read the puzzle description on Advent of Code
Check out the solution from Sebastian Aigner on the Kotlin Blog or watch the video:
Day 5: Binary boarding
Use the Kotlin standard library functions (replace()
, toInt()
, find()
) to work with the binary representation of numbers, explore powerful local functions, and learn how to use the max()
function in Kotlin 1.5.
Read the puzzle description on Advent of Code
Check out the solution from Svetlana Isakova on the Kotlin Blog or watch the video:
Day 6: Custom customs
Learn how to group and count characters in strings and collections using the standard library functions: map()
, reduce()
, sumOf()
, intersect()
, and union()
.
Read the puzzle description on Advent of Code
Check out the solution from Anton Arhipov on the Kotlin Blog or watch the video:
Day 7: Handy haversacks
Learn how to use regular expressions, use Java's compute()
method for HashMaps from Kotlin for dynamic calculations of the value in the map, use the forEachLine()
function to read files, and compare two types of search algorithms: depth-first and breadth-first.
Read the puzzle description on Advent of Code
Check out the solution from Pasha Finkelshteyn on the Kotlin Blog or watch the video:
Day 8: Handheld halting
Apply sealed classes and lambdas to represent instructions, apply Kotlin sets to discover loops in the program execution, use sequences and the sequence { }
builder function to construct a lazy collection, and try the experimental measureTimedValue()
function to check performance metrics.
Read the puzzle description on Advent of Code
Check out the solution from Sebastian Aigner on the Kotlin Blog or watch the video:
Day 9: Encoding error
Explore different ways to manipulate lists in Kotlin using the any()
, firstOrNull()
, firstNotNullOfOrNull()
, windowed()
, takeIf()
, and scan()
functions, which exemplify an idiomatic Kotlin style.
Read the puzzle description on Advent of Code
Check out the solution from Svetlana Isakova on the Kotlin Blog or watch the video:
What's next?
Complete more tasks with Kotlin Koans
Create working applications with the free Kotlin Core track by JetBrains Academy