Day 7 Part 2 best brute force optimization without using dynamic programming.
This commit is contained in:
parent
8d492486fb
commit
15ce1ad06e
@ -3,7 +3,6 @@ package aoc2024
|
|||||||
import println
|
import println
|
||||||
import readInput
|
import readInput
|
||||||
import splitLongs
|
import splitLongs
|
||||||
import kotlin.math.pow
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
--- Day 7: Bridge Repair ---
|
--- Day 7: Bridge Repair ---
|
||||||
@ -81,15 +80,18 @@ fun main() {
|
|||||||
val numbers = i.splitLongs(" ", ":")
|
val numbers = i.splitLongs(" ", ":")
|
||||||
val eqres = numbers[0]
|
val eqres = numbers[0]
|
||||||
val ops = numbers.drop(1)
|
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) {
|
for (t in 0 until combs) {
|
||||||
val res = ops.reduceIndexed { index, acc, l ->
|
var res = ops[0]
|
||||||
when ((t / powThreeTable[index - 1]) % 3) {
|
for (index in 1 until ops.size) {
|
||||||
0 -> acc + l
|
val l = ops[index]
|
||||||
1 -> acc * l
|
res = when ((t / powThreeTable[index - 1]) % 3) {
|
||||||
2 -> acc * powTenTable[fastCeilLog10(l)] + l
|
0 -> res + l
|
||||||
else -> throw IllegalArgumentException()
|
1 -> res * l
|
||||||
|
2 -> res * powTenTable[fastCeilLog10(l)] + l
|
||||||
|
else -> 0
|
||||||
}
|
}
|
||||||
|
if (res > eqres) break
|
||||||
}
|
}
|
||||||
if (res == eqres) {
|
if (res == eqres) {
|
||||||
sum += eqres
|
sum += eqres
|
||||||
|
Loading…
Reference in New Issue
Block a user