This commit is contained in:
Sven Weidauer 2024-12-09 20:54:32 +01:00
parent eb6879f5a3
commit dc670cfe15
2 changed files with 884 additions and 0 deletions

View file

@ -0,0 +1,34 @@
data class Equation(val test: Long, val numbers: List<Long>) {
fun canSolve(): Boolean = trySolve(numbers[0], numbers.drop(1))
private fun trySolve(already: Long, rest: List<Long>): Boolean = when {
rest.isEmpty() -> already == test
already > test -> false
else -> trySolve(already + rest.first(), rest.drop(1)) ||
trySolve(already * rest.first(), rest.drop(1))
}
fun canSolve2(): Boolean = trySolve2(numbers[0], numbers.drop(1))
private fun trySolve2(already: Long, rest: List<Long>): Boolean = when {
rest.isEmpty() -> already == test
already > test -> false
else -> trySolve2(already + rest.first(), rest.drop(1)) ||
trySolve2(already * rest.first(), rest.drop(1)) ||
trySolve2("$already${rest.first()}".toLong(), rest.drop(1))
}
}
fun main() {
val equations = readInput("day7.txt").map { line ->
val (test, equation) = line.split(": ", limit = 2)
Equation(test.toLong(), equation.split(" ").map { it.toLong() })
}.toList()
val part1 = equations.filter { it.canSolve() }.sumOf { it.test }
println("Part 1: $part1")
val part2 = equations.filter { it.canSolve2() }.sumOf { it.test }
println("Part 2: $part2")
}