Kotlin
I first solved the puzzle with my own horrible range-merging algorithm, had quite a few nasty off by one errors. I quickly replaced it with the "normal" merge-algorithm after getting the right solution.
If you really want to see my abomination, it's still in the commits on my repo.
Solution
class Day05 : Puzzle {
val validIds = mutableSetOf<LongRange>()
val ids = mutableListOf<Long>()
override fun readFile() {
val input = readInputFromFile("src/main/resources/a2025/day05.txt")
val inputList = input.split("\n\n", limit = 2)
validIds.addAll(
inputList[0].lines().filter { it.isNotBlank() }
.map { it.split("-").map { it.toLong() } }
.map { LongRange(it[0], it[1]) }
)
ids.addAll(inputList[1].lines().filter { it.isNotBlank() }.map { it.toLong() })
}
override fun solvePartOne(): String {
return ids.count { id -> validIds.any { id in it } }.toString()
}
override fun solvePartTwo(): String {
val idsSorted = validIds.sortedBy { it.first }
val merged = mutableSetOf<LongRange>()
var current = idsSorted.first()
for (i in 1..<idsSorted.size) {
val next = idsSorted[i]
if (next.first <= current.last) {
current = LongRange(current.first, max(current.last, next.last()))
} else {
merged.add(current)
current = next
}
}
merged.add(current)
return merged.sumOf { it.last + 1 - it.first }.toString()
}
}
full code on Codeberg