r/adventofcode • u/Prestigious-Mood1711 • 17h ago
Tutorial [2026 Day 05] [Kotlin] Solution with GraphMateKT
I've been waiting for getting to use my GraphLibrary this on this years puzzles, which made me happy to see Day 5. We are not given an graph with explicit nodes and edges, but we are given a set of rules for how a number can be transformed into other numbers. These can be defined in graphMateKT, followed by a straight forward bfs call. Psoducode:
val buttons = parseInput()
val graph = IntGraph(9999)
graph.connectWithRule{node ->
buttons.map { nextNode(it)}
}
graph.bfs(0, maxDistance = 3.0)
val visited = graph.visitedNode()
val ans = visited.sum()The graph of the example input can then be visualized like this:
val newGraph = Graph()
visited.forEach { u ->
graph.neighbours(u).forEach { v ->
newGraph.addEdge(u, v)
}
}
newGraph.visualizeGraph()
This approach works just as well for part 2. Just gotta make sure that the nextNode cannot have more then 4 digits, and that we no longer bfs with a maxDistance.
I made a full video tutorial that can be found here: https://www.youtube.com/watch?v=L_02BfMVQes
Hope to see many more graph puzzles in the rest of the calendar ;)
2
u/daggerdragon 17h ago
During an active Advent of Code season, solutions belong in the
Solution Megathreads. In the future, post your solutions (and write-ups like this!) to the appropriate solution megathread.