Kotlin
got up early for this one, sadly it took me over 30 minutes to realize, that my code at the time was also considering a single digit valid .-.
also still pretty scuffed, but hey it works:
Solution
class Day02 : Puzzle {
val ids = mutableSetOf<String>()
override fun readFile() {
val input = readInputFromFile("src/main/resources/a2025/day02.txt")
ids.addAll(
input.replace("\n", "")
.split(",")
.map { it.split("-") }
.map(this::buildList)
.flatten()
)
}
private fun buildList(rangeList: List<String>): List<String> {
val start = rangeList[0].toLong()
val end = rangeList[1].toLong()
val ids = mutableListOf<String>()
for (i in start..end) {
ids.add(i.toString())
}
return ids
}
override fun solvePartOne(): String {
return ids.filter(this::idNotValid)
.sumOf(String::toLong).toString()
}
override fun solvePartTwo(): String {
return ids.filter { idNotValid(it, true) }
.sumOf(String::toLong).toString()
}
private fun idNotValid(id: String, multipleSplits: Boolean = false): Boolean {
val length = id.length
// try all splits
var split = 2
while (split <= length) {
if (length % split == 0) {
val splits = mutableListOf<String>()
var beg = 0
var end = length / split
val step = end
for (i in 0..<split) {
splits.add(id.substring(beg, end))
beg += step
end += step
}
if (splits.all { it == splits[0] }) return true
}
if (multipleSplits) {
split++
} else {
break
}
}
return false
}
}
full code on Codeberg