aoc.gabber235.dev

Day 1

kotlin
puzzle ↗

bench

v2 is the fastest

part 1

65.0 µs

62.6 µs 87.5 µs

counttop 3

rewrites

v1
1.2xv2
fastestv3
6.1x

warm-up 3.7x faster once warm

215 KB allocated per run

part 2

63.9 µs

61.8 µs 87.2 µs

counttop 3

rewrites

v1
1.2xv2
fastestv3
6.0x

warm-up 5.3x faster once warm

215 KB allocated per run

code · v2

2026/days/day01/v2/Day01.kt
package day01.v2

import aoc.section
import aoc.solve

const val SIZE = 3

inline fun solve(input: String, start: Int, compare: (Int, Int) -> Boolean): Int {
    val counts = section("count") {
        val counts = mutableMapOf<Int, Int>()
        for (number in input.lineSequence().map { it.toInt() }) {
            counts.compute(number) { _, v -> if (v == null) 1 else v + 1 }
        }
        counts
    }

    val targets = IntArray(SIZE) { -1 }
    val targetCounts = IntArray(SIZE) { start }

    section("top $SIZE") {
        for ((key, value) in counts) {
            for (i in 0 until SIZE) {
                if (!compare(value, targetCounts[i])) break
                if (i > 0) {
                    targets[i - 1] = targets[i]
                    targetCounts[i - 1] = targetCounts[i]
                }
                targets[i] = key
                targetCounts[i] = value
            }
        }
    }

    return targets.reduce { acc, i -> acc * i  }

}

fun part1(input: String): Any? {
    return solve(input, -1) { a, b -> a > b }
}

fun part2(input: String): Any? {
    return solve(input, 1000) { a, b -> a < b }
}

fun main() = solve(1, version = 2)
Puzzles by Eric Wastl at adventofcode.com · solutions and benchmarks by gabber235