Skip to content

Commit 007466c

Browse files
committed
[2025/10] Factory (Part 2)
1 parent a38dd16 commit 007466c

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
| 2022 |||||||||||||| | | | | | | || | | || 28 |
3030
| 2023 |||||||| | | | | | | | | | | | | | | | | | | 12 |
3131
| 2024 ||||||||||||||| |||||| || | || 40 |
32-
| 2025 |||||||||| |||||||||||||||| 22 |
32+
| 2025 |||||||||| |||||||||||||||| 23 |
3333

3434
## 🛷 How to run
3535

@@ -215,7 +215,7 @@ e.g. `HandyHaversacks`)*
215215
| | 7 | [Laboratories](https://adventofcode.com/2025/day/7) | [[Code](src/main/kotlin/adventofcode/year2025/Day07Laboratories.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day07LaboratoriesSpec.kt)] | `1711` | `36706966158365` |
216216
| | 8 | [Playground](https://adventofcode.com/2025/day/8) | [[Code](src/main/kotlin/adventofcode/year2025/Day08Playground.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day08PlaygroundSpec.kt)] | `135169` | `302133440` |
217217
| | 9 | [Movie Theater](https://adventofcode.com/2025/day/9) | [[Code](src/main/kotlin/adventofcode/year2025/Day09MovieTheater.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day09MovieTheaterSpec.kt)] | `4777409595` | `1473551379` |
218-
| | 10 | [Factory](https://adventofcode.com/2025/day/10) | [[Code](src/main/kotlin/adventofcode/year2025/Day10Factory.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day10FactorySpec.kt)] | `517` | |
218+
| | 10 | [Factory](https://adventofcode.com/2025/day/10) | [[Code](src/main/kotlin/adventofcode/year2025/Day10Factory.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day10FactorySpec.kt)] | `517` | `21469` |
219219
| | 11 | [Reactor](https://adventofcode.com/2025/day/11) | [[Code](src/main/kotlin/adventofcode/year2025/Day11Reactor.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day11ReactorSpec.kt)] | `699` | `388893655378800` |
220220
| | 12 | [Christmas Tree Farm](https://adventofcode.com/2025/day/12) | [[Code](src/main/kotlin/adventofcode/year2025/Day12ChristmasTreeFarm.kt)] | `524` | |
221221

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.20.1")
2828
implementation("org.reflections:reflections:0.10.2")
2929
implementation("org.slf4j:slf4j-nop:2.0.17")
30+
implementation("tools.aqua:z3-turnkey:4.14.1")
3031

3132
testImplementation("io.kotest:kotest-runner-junit5-jvm:6.0.7")
3233
testImplementation("io.kotest:kotest-assertions-core-jvm:6.0.7")

src/main/kotlin/adventofcode/year2025/Day10Factory.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package adventofcode.year2025
33
import adventofcode.Puzzle
44
import adventofcode.PuzzleInput
55
import adventofcode.common.Permutations.powersets
6+
import com.microsoft.z3.Context
7+
import com.microsoft.z3.Status.SATISFIABLE
68

79
class Day10Factory(
810
customInput: PuzzleInput? = null,
@@ -28,6 +30,46 @@ class Day10Factory(
2830
}.size
2931
}
3032

33+
override fun partTwo() =
34+
parseInput()
35+
.sumOf { machine ->
36+
Context().use { ctx ->
37+
val solver = ctx.mkOptimize()
38+
39+
// Create variables for button presses and ensure they are positive.
40+
val buttons =
41+
machine.buttons.indices
42+
.map { index -> ctx.mkIntConst("button#$index") }
43+
.onEach { button -> solver.Add(ctx.mkGe(button, ctx.mkInt(0))) }
44+
45+
// Create equations: For each joltage counter, ensure the sum of all button presses is equal to the specified joltage
46+
machine.joltages.forEachIndexed { counter, joltage ->
47+
val pressedButtons =
48+
machine.buttons
49+
.withIndex()
50+
.filter { (_, counters) -> counter in counters }
51+
.map { buttons[it.index] }
52+
53+
solver.Add(ctx.mkEq(ctx.mkAdd(*pressedButtons.toTypedArray()), ctx.mkInt(joltage)))
54+
}
55+
56+
// Solve for the minimum number of button presses
57+
val buttonPresses = ctx.mkIntConst("buttonPresses")
58+
solver.Add(ctx.mkEq(buttonPresses, ctx.mkAdd(*buttons.toTypedArray())))
59+
solver.MkMinimize(buttonPresses)
60+
61+
when (solver.Check()) {
62+
SATISFIABLE ->
63+
solver.model
64+
.evaluate(buttonPresses, false)
65+
.toString()
66+
.toInt()
67+
68+
else -> 0
69+
}
70+
}
71+
}
72+
3173
companion object {
3274
private data class Machine(
3375
val lights: List<Boolean>,

src/test/kotlin/adventofcode/year2025/Day10FactorySpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package adventofcode.year2025
22

33
import adventofcode.PuzzleBaseSpec
44

5-
class Day10FactorySpec : PuzzleBaseSpec(7)
5+
class Day10FactorySpec : PuzzleBaseSpec(7, 33)

0 commit comments

Comments
 (0)