Day 7 Part 2 best brute force optimization without using dynamic programming.

This commit is contained in:
Chris Hodges 2024-12-07 07:20:08 +01:00
parent 8d492486fb
commit 15ce1ad06e

View File

@ -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