Sanniv Khare 7 min read

Advent of Code 2026 in Kotlin: a Gradle starter with tests

Advent of Code 2026 in Kotlin: a Gradle starter with tests

Advent of Code 2026 starts on 1 December, and a Kotlin setup needs only five files and the Gradle wrapper: build.gradle.kts with the Kotlin JVM plugin and application, a Day interface, one class per day, a Map that registers them, and a JUnit test per day fed with the puzzle’s sample input. We built the starter below with Kotlin 2.4.21, Gradle 9.4.1, JUnit 5.13.4 and JDK 17 (Zulu 17.0.21) on our M1 lab Mac. The first ./gradlew clean test with a fresh Gradle daemon took 28 s; after that, rerunning the tests took 832 ms. Use the --rerun flag when you want to watch the tests pass again: a plain ./gradlew test with nothing changed skips the task.

Why we did this: Kotlin is the language our Android work runs on (BMDB’s TV app, Peepal, Shopkeeper), and Advent of Code is how a lot of the team keeps it sharp in December. It is the Kotlin twin of our Swift and SwiftPM starter, with the same warm-up puzzle so the two can be compared.

The layout

aoc/
  build.gradle.kts
  settings.gradle.kts
  gradle.properties
  gradlew, gradle/wrapper/
  gen-input.mjs
  src/main/kotlin/aoc/
    Day.kt      interface + registry
    Day00.kt    warm-up day
    Day01.kt    template for a new day
    Main.kt     ./gradlew run entry
  src/test/kotlin/aoc/
    Day00Test.kt
  inputs/       your puzzle inputs (git-ignored)

Keep inputs/ out of git. The Advent of Code about page asks people not to publish the puzzle text or their inputs, so a public repo should carry code and the sample-based tests only.

build.gradle.kts

import org.gradle.api.tasks.testing.logging.TestExceptionFormat

plugins {
    kotlin("jvm") version "2.4.21"
    application
}

repositories { mavenCentral() }

dependencies {
    testImplementation(kotlin("test"))
    testImplementation(platform("org.junit:junit-bom:5.13.4"))
    testImplementation("org.junit.jupiter:junit-jupiter-params")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

application { mainClass.set("aoc.MainKt") }

tasks.test {
    useJUnitPlatform()
    testLogging {
        events("passed", "failed", "skipped")
        exceptionFormat = TestExceptionFormat.FULL
    }
}

Three things in there are deliberate:

  • junit-platform-launcher on testRuntimeOnly, versions from the JUnit BOM. Gradle’s JUnit 5 setup declares the launcher explicitly, and the BOM keeps it in step with Jupiter. We also built a copy of the project without this line: on Gradle 9.4.1 with kotlin("test") the six tests still passed (clean build in 2 s with a warm daemon), so treat it as the documented setup rather than a fix you will see fail.
  • junit-jupiter-params. kotlin("test") brings JUnit 5 assertions through kotlin-test-junit5, but parameterized tests live in a separate artifact.
  • testLogging. Gradle prints nothing for passing tests by default. With events("passed", ...) you see every case, which is what you want at 6 a.m. on puzzle day.

settings.gradle.kts is one line, rootProject.name = "aoc". Use the Gradle wrapper (we used 9.4.1) rather than a system Gradle, so the build runs the same on every machine. Note that ./gradlew --version reports Kotlin 2.3.0: that is the Kotlin embedded in Gradle for build scripts. Your code compiles with the plugin version in plugins {}, 2.4.21 here, and the distribution’s lib/ folder confirms it with kotlin-stdlib-2.4.21.jar.

One class per day

package aoc

/** One puzzle day. Parse once in the constructor, answer both parts. */
interface Day {
    fun part1(): String
    fun part2(): String
}

/** The registry: add a line per day. */
val days: Map<Int, (String) -> Day> = mapOf(
    0 to ::Day00,
    1 to ::Day01,
)

/** Lines of the input without the trailing newline. */
fun lines(input: String): List<String> =
    input.removeSuffix("\n").let { if (it.isEmpty()) emptyList() else it.split("\n") }

Answers are String because some days ask for a number and some for a code or a word; comparing strings in tests keeps one shape for both. The constructor reference ::Day00 is the factory, so adding a day is a new file plus one line in the map.

Swift and Kotlin versions of the same Advent of Code starter, side by side

The Swift starter uses a protocol with init(input:); in Kotlin the constructor does that job.

The warm-up day is our own small puzzle, not an Advent of Code one, so it can be shown in full: each line is x,y; part 1 sums the Manhattan distances from the origin, part 2 counts distinct points.

package aoc

import kotlin.math.abs

class Day00(input: String) : Day {
    data class Point(val x: Int, val y: Int)

    private val points: List<Point> = lines(input).mapNotNull { line ->
        val parts = line.split(",")
        if (parts.size != 2) return@mapNotNull null
        val x = parts[0].toIntOrNull() ?: return@mapNotNull null
        val y = parts[1].toIntOrNull() ?: return@mapNotNull null
        Point(x, y)
    }

    override fun part1(): String = points.sumOf { abs(it.x) + abs(it.y) }.toString()

    override fun part2(): String = points.toSet().size.toString()
}

Day01.kt is the template you copy each morning: it reads the lines and returns “not solved yet” for both parts until you fill them in.

Tests from the sample input

Every puzzle comes with a small example and its expected answer. That example is the test:

package aoc

import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

private val sample = """
    3,4
    -2,1
    3,4
    0,-5
""".trimIndent() + "\n"

class Day00Test {
    @Test fun part1() = assertEquals("22", Day00(sample).part1())

    @Test fun part2() = assertEquals("3", Day00(sample).part2())

    @ParameterizedTest
    @CsvSource(delimiter = '|', value = ["1,1|2", "-3,0\\n0,3|6", "''|0"])
    fun part1Cases(input: String, expected: String) =
        assertEquals(expected, Day00(input.replace("\\n", "\n")).part1())

    @Test fun registry() {
        assertNotNull(days[0])
        assertNull(days[25])
    }
}

Two small tricks: delimiter = '|' lets the CSV values contain commas, which puzzle input usually does, and '' is how @CsvSource passes an empty string (an empty input should give 0, not crash).

Gradle test output: six tests passed, 28 s on the first run and 832 ms on rerun

Real output from our M1 lab Mac. The 28 s includes starting a fresh Gradle daemon and the first Kotlin compile.

What the loop costs

All numbers below are from one run each on our M1 lab Mac (16 GB, macOS 15.7.3, JDK 17.0.21), with dependencies already in the Gradle cache. They show the shape of the loop, not a benchmark:

Command Result
./gradlew clean test (fresh daemon) 28 s, 6 tests passed
./gradlew test (nothing changed) 436 ms, :test UP-TO-DATE
./gradlew test --rerun (warm daemon) 832 ms, 6 tests passed
./gradlew installDist aoc.jar 11,646 bytes plus kotlin-stdlib 1,853,315 bytes

The middle row matters more than it looks: if you run test without changing anything, Gradle skips the task and prints no test lines at all. Use --rerun (Gradle 7.6 and later) when you want to see them pass again.

Running a day, with timings

Main.kt takes the day, an optional input file and an optional number of rounds, and times each part with measureTimedValue from the standard library:

val (solver, tParse) = measureTimedValue { make(input) }
val (p1, t1) = measureTimedValue { solver.part1() }
val (p2, t2) = measureTimedValue { solver.part2() }
println("Day $n round ${r + 1}  parse  $tParse")

./gradlew run starts the program in the project folder, so relative paths like inputs/day01.txt work. We generated a 200,000-line input for the warm-up day (the same seeded generator as the Swift starter) and ran three rounds in one JVM:

./gradlew -q run --args="0 inputs/day00.txt 3"
Day 0 round 1  parse  70.664083ms
Day 0 round 1  part 1 201026463  (8.467209ms)
Day 0 round 1  part 2 5452  (13.237208ms)
Day 0 round 2  parse  32.156ms
Day 0 round 2  part 1 201026463  (3.660959ms)
Day 0 round 2  part 2 5452  (3.333834ms)
Day 0 round 3  parse  17.196500ms
Day 0 round 3  part 1 201026463  (2.379333ms)
Day 0 round 3  part 2 5452  (7.315958ms)

Parsing fell from 71 ms to 17 ms by the third round as the JIT warmed up. That is why the rounds argument exists: a single cold timing on the JVM mostly measures startup, so if you compare two solutions, compare a later round. The answers match the Swift version on the same input.

An unknown day exits with code 65 (“no solution registered for day 25”), which Gradle reports as a failed :run task. Missing input files exit with 66, and a bad argument prints the usage line.

Each morning in December

  1. Copy Day01.kt to DayNN.kt, rename the class and add NN to ::DayNN to the map.
  2. Paste the puzzle’s example and expected answer into DayNNTest.kt.
  3. Save your input as inputs/dayNN.txt (never commit it).
  4. Run ./gradlew test until the example passes, then ./gradlew -q run --args="NN".

The daemon stays warm between runs, so after the first build of the day each test cycle is around a second on our Mac.

Made by the team behind this post

Apps from our studio

  • Goes with this post

    BMDB.fun

    Your Bollywood world at a glance, with links straight to the OTT app that streams each film.

    Android TV · Web · Mobile

    Get
  • Nix

    The Mac uninstaller that finds what the Trash leaves behind: caches, logs, derived data and saved states.

    macOS

    Get
  • Cardigami

    Pop-up greeting cards that rise out of the fold on iPhone Duo.

    iPhone · App Store

    Get
  • OneClick QuitAll

    Quit every running app in one click. See memory and CPU first.

    macOS · Mac App Store

    Get

Need this built? Let’s talk.

We build and run apps for Mac, TV, mobile and the web, and write about what we learn.