Still non-working part 2. But I have an idea.
This commit is contained in:
parent
ccc3fdad4c
commit
7495f9e378
@ -1,8 +1,10 @@
|
|||||||
package aoc2025
|
package aoc2025
|
||||||
|
|
||||||
|
import calcCanonicalPrimesSieveOfEratosthenes
|
||||||
import println
|
import println
|
||||||
import readInput
|
import readInput
|
||||||
import splitInts
|
import splitInts
|
||||||
|
import java.math.BigInteger
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -29,7 +31,6 @@ fun main() {
|
|||||||
val toggles =
|
val toggles =
|
||||||
stuff.drop(1).dropLast(1)
|
stuff.drop(1).dropLast(1)
|
||||||
.map { it.removeSurrounding("(", ")").splitInts(",").fold(0) { acc, v -> acc + (1 shl v) } }
|
.map { it.removeSurrounding("(", ")").splitInts(",").fold(0) { acc, v -> acc + (1 shl v) } }
|
||||||
.sortedByDescending { it.countOneBits() }
|
|
||||||
.toIntArray()
|
.toIntArray()
|
||||||
val joltages = stuff.last().removeSurrounding("{", "}").splitInts(",").toIntArray()
|
val joltages = stuff.last().removeSurrounding("{", "}").splitInts(",").toIntArray()
|
||||||
machines.add(Machine(machSize, target, toggles, joltages))
|
machines.add(Machine(machSize, target, toggles, joltages))
|
||||||
@ -93,6 +94,63 @@ fun main() {
|
|||||||
fun part2(input: List<String>): Int {
|
fun part2(input: List<String>): Int {
|
||||||
val machines = parse(input)
|
val machines = parse(input)
|
||||||
|
|
||||||
|
val primes = calcCanonicalPrimesSieveOfEratosthenes(10000)
|
||||||
|
var sumButts = 0
|
||||||
|
for (m in machines) {
|
||||||
|
println()
|
||||||
|
val maxJoltage = m.joltage.max()
|
||||||
|
val numBits = 32 - maxJoltage.countLeadingZeroBits()
|
||||||
|
val bigTarget = m.joltage.foldIndexed(BigInteger.ZERO) { index, acc, i -> acc.plus(BigInteger.valueOf(i.toLong()).shiftLeft(numBits * index)) }
|
||||||
|
val bigToggles = ArrayList<Pair<BigInteger, BigInteger>>()
|
||||||
|
for (t in m.toggles) {
|
||||||
|
var tt = t
|
||||||
|
var bigToggle = BigInteger.ZERO
|
||||||
|
var shift = 0
|
||||||
|
while (tt > 0) {
|
||||||
|
if (tt and 1 != 0) {
|
||||||
|
bigToggle = bigToggle.plus(BigInteger.ONE.shiftLeft(shift))
|
||||||
|
}
|
||||||
|
shift += numBits
|
||||||
|
tt = tt shr 1
|
||||||
|
}
|
||||||
|
val maxValue = bigTarget.divide(bigToggle).and(BigInteger.ONE.shiftLeft(numBits).minus(BigInteger.ONE)).toInt() + 1
|
||||||
|
println(maxValue)
|
||||||
|
if (maxValue > 0) {
|
||||||
|
bigToggles.add(bigToggle to BigInteger.valueOf(maxValue.toLong()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var presses = BigInteger.ONE
|
||||||
|
var leastFingers = Integer.MAX_VALUE
|
||||||
|
val maxPresses = bigToggles.fold(BigInteger.ONE) { acc, bt -> acc.multiply(bt.second) }
|
||||||
|
while (presses <= maxPresses) {
|
||||||
|
var pe = 0
|
||||||
|
var pr = presses
|
||||||
|
var bt = bigTarget
|
||||||
|
var fingers = 0
|
||||||
|
while (pe < bigToggles.size && pr > BigInteger.ZERO) {
|
||||||
|
val f = pr.mod(bigToggles[pe].second)
|
||||||
|
fingers += f.toInt()
|
||||||
|
if (fingers > leastFingers) break
|
||||||
|
bt = bt.minus(bigToggles[pe].first.multiply(f))
|
||||||
|
if (bt <= BigInteger.ZERO) break
|
||||||
|
pr = pr.divide(bigToggles[pe].second)
|
||||||
|
pe++
|
||||||
|
}
|
||||||
|
if (bt == BigInteger.ZERO) {
|
||||||
|
leastFingers = leastFingers.coerceAtMost(fingers)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
presses = presses.plus(BigInteger.ONE)
|
||||||
|
}
|
||||||
|
println(leastFingers)
|
||||||
|
sumButts += leastFingers
|
||||||
|
}
|
||||||
|
return sumButts
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part2old(input: List<String>): Int {
|
||||||
|
val machines = parse(input)
|
||||||
|
|
||||||
var sumButts = 0
|
var sumButts = 0
|
||||||
for (m in machines) {
|
for (m in machines) {
|
||||||
// generate toggles and calculate the global maximum of toggle presses for this toggle
|
// generate toggles and calculate the global maximum of toggle presses for this toggle
|
||||||
@ -129,12 +187,7 @@ fun main() {
|
|||||||
|
|
||||||
// press all buttons regarding their minimal count (if any)
|
// press all buttons regarding their minimal count (if any)
|
||||||
for (t in toggles) {
|
for (t in toggles) {
|
||||||
if (pressCount[t.idx] + t.min > t.max || !applyJoltage(
|
if (pressCount[t.idx] + t.min > t.max || !applyJoltage(jolts, t, times = t.min)) throw IllegalStateException()
|
||||||
jolts,
|
|
||||||
t,
|
|
||||||
times = t.min
|
|
||||||
)
|
|
||||||
) throw IllegalStateException()
|
|
||||||
pressCount[t.idx] += t.min
|
pressCount[t.idx] += t.min
|
||||||
}
|
}
|
||||||
// this is the starting point for the exhaustive search
|
// this is the starting point for the exhaustive search
|
||||||
@ -151,7 +204,7 @@ fun main() {
|
|||||||
}
|
}
|
||||||
for (t in toggles) {
|
for (t in toggles) {
|
||||||
if (tc[t.idx] + 2 <= t.max) {
|
if (tc[t.idx] + 2 <= t.max) {
|
||||||
val maxTimes = findMaxButtonPresses(j, t)
|
val maxTimes = 1//findMaxButtonPresses(j, t)
|
||||||
if (maxTimes > 0) {
|
if (maxTimes > 0) {
|
||||||
val nj = j.copyOf()
|
val nj = j.copyOf()
|
||||||
if (applyJoltage(nj, t, maxTimes)) {
|
if (applyJoltage(nj, t, maxTimes)) {
|
||||||
@ -178,7 +231,7 @@ fun main() {
|
|||||||
val testInputPart2Result = part2(testInput)
|
val testInputPart2Result = part2(testInput)
|
||||||
println("Part 2 Test: $testInputPart2Result")
|
println("Part 2 Test: $testInputPart2Result")
|
||||||
check(testInputPart1Result == 7)
|
check(testInputPart1Result == 7)
|
||||||
//check(testInputPart2Result == 33)
|
check(testInputPart2Result == 33)
|
||||||
|
|
||||||
val input = readInput("aoc2025/Day10")
|
val input = readInput("aoc2025/Day10")
|
||||||
part1(input).println()
|
part1(input).println()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user