diff --git a/src/aoc2024/Day07.kt b/src/aoc2024/Day07.kt index 4ff38d8..be60ee4 100644 --- a/src/aoc2024/Day07.kt +++ b/src/aoc2024/Day07.kt @@ -3,7 +3,6 @@ package aoc2024 import println import readInput import splitLongs -import kotlin.math.pow /* --- Day 7: Bridge Repair --- @@ -81,15 +80,18 @@ fun main() { val numbers = i.splitLongs(" ", ":") val eqres = numbers[0] val ops = numbers.drop(1) - val combs = 3.0.pow(ops.size - 1).toInt() + val combs = powThreeTable[ops.size - 1] for (t in 0 until combs) { - val res = ops.reduceIndexed { index, acc, l -> - when ((t / powThreeTable[index - 1]) % 3) { - 0 -> acc + l - 1 -> acc * l - 2 -> acc * powTenTable[fastCeilLog10(l)] + l - else -> throw IllegalArgumentException() + var res = ops[0] + for (index in 1 until ops.size) { + val l = ops[index] + res = when ((t / powThreeTable[index - 1]) % 3) { + 0 -> res + l + 1 -> res * l + 2 -> res * powTenTable[fastCeilLog10(l)] + l + else -> 0 } + if (res > eqres) break } if (res == eqres) { sum += eqres