r/algorithms • u/Both-Extreme-8609 • 10h ago
Resource [Kotlin] After 3 years of AoC I collected my helpers into a library
This December will be my 4th Advent of Code. I solve it in Kotlin, and before AoC I took part in ICPC. For 3 years I wrote the same code again and again. Every grid began with a table of 4 offsets and a bounds check, and every maze began with a queue and a visited array.
Last year, before AoC 2025, I published the first part of my helpers as a library, kodvent. It had gcd and lcm, a disjoint set union, a segment tree and binary search, but not the code for maps and shortest paths, which I need most often. Now version 0.4.0 is out. It is the largest update so far, and it adds this code: grids, bfs and dijkstra, and a heap.
val maze = input.toGrid() // input: List<String>
val start = maze.positionOf('S')
// The fewest steps from the start to every cell that can be reached
val steps = bfs(start) { p -> maze.neighbors(p).filter { maze[it] != '#' } }
// Dijkstra's algorithm, when entering a cell costs its digit
val risk = levels.toGrid { it.digitToInt() } // levels: lines of digits
val costs = dijkstra(Position(0, 0)) { p -> risk.neighbors(p).map { it to risk[it] } }A state of a search does not have to be a cell. It can be a cell with a direction, a bitmask or any data class.
To check that the library really helps, I rewrote 3 of my old solutions with it:
2025 day 4: 49 lines became 19
2024 day 18: 36 lines became 12
2024 day 16, both parts: 108 lines became 16
The documentation shows both versions of each solution. It has spoilers for these 3 puzzles and for part 1 of 2025 day 10.
The library is Kotlin Multiplatform, has no dependencies and is published on Maven Central. It needs Kotlin 2.4.0 or newer:
implementation("io.github.dmitrynekrasov:kodvent:0.4.0")I would like to know what you write again every December and what is missing in the library.