Skip to content

Commit 705dee7

Browse files
committed
Add broken free variable brute force to Gaussian elimination solution in 2025 day 10 part 2
1 parent 5afa598 commit 705dee7

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

src/main/scala/eu/sim642/adventofcode2025/Day10.scala

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package eu.sim642.adventofcode2025
22

3+
import eu.sim642.adventofcodelib.IteratorImplicits._
4+
35
import com.microsoft.z3.{ArithExpr, Context, IntExpr, IntSort, Status}
46
import eu.sim642.adventofcodelib.graph.{BFS, Dijkstra, GraphSearch, TargetNode, UnitNeighbors}
57

@@ -164,21 +166,56 @@ object Day10 {
164166
for (y2 <- y until m.size)
165167
assert(m(y2).last == 0)
166168

169+
val mainVars = mutable.ArrayBuffer.empty[Int]
170+
val freeVars = mutable.ArrayBuffer.empty[Int]
167171
y = 0
168172
for (x <- machine.buttons.indices) {
169173
if (y < m.size) { // TODO: break if y too big
170-
if (m(y)(x) == 0)
171-
() // move to next x
174+
if (m(y)(x) == 0) {
175+
freeVars += x
176+
()
177+
} // move to next x
172178
else {
179+
mainVars += x
173180
for (y3 <- 0 until y)
174181
reduceUp(x, y, y3)
175182

176183
y += 1
177184
}
178185
}
186+
else
187+
freeVars += x // can't break if this is here
188+
}
189+
190+
def helper0(sum: Int, len: Int): Iterator[List[Int]] = {
191+
if (len == 0)
192+
Iterator(Nil)
193+
else if (len == 1)
194+
Iterator(List(sum))
195+
else {
196+
for {
197+
x <- (0 to sum).iterator
198+
rest <- helper0(sum - x, len - 1)
199+
} yield x :: rest
200+
}
179201
}
180202

181-
???
203+
val choices = Iterator.from(0).flatMap(helper0(_, freeVars.size))
204+
val answer =
205+
choices
206+
.map(freeVals => {
207+
val mainVals = mainVars.view.zipWithIndex.map((mainVar, y) => {
208+
val row = m(y)
209+
row.last - (freeVars lazyZip freeVals).map((freeVar, freeVal) => row(freeVar) * freeVal).sum
210+
}).toList
211+
(mainVals, freeVals)
212+
})
213+
.filter(_._1.forall(_ >= 0)) // all main vals must be non-negative
214+
.map((s1, s2) => s1.sum + s2.sum)
215+
.head // TODO: wrong, freeVals sum is minimal, but mainVals sum isn't
216+
217+
println(answer)
218+
answer
182219
}
183220
}
184221

0 commit comments

Comments
 (0)