|
1 | 1 | package eu.sim642.adventofcode2025 |
2 | 2 |
|
| 3 | +import eu.sim642.adventofcodelib.IteratorImplicits._ |
| 4 | + |
3 | 5 | import com.microsoft.z3.{ArithExpr, Context, IntExpr, IntSort, Status} |
4 | 6 | import eu.sim642.adventofcodelib.graph.{BFS, Dijkstra, GraphSearch, TargetNode, UnitNeighbors} |
5 | 7 |
|
@@ -164,21 +166,56 @@ object Day10 { |
164 | 166 | for (y2 <- y until m.size) |
165 | 167 | assert(m(y2).last == 0) |
166 | 168 |
|
| 169 | + val mainVars = mutable.ArrayBuffer.empty[Int] |
| 170 | + val freeVars = mutable.ArrayBuffer.empty[Int] |
167 | 171 | y = 0 |
168 | 172 | for (x <- machine.buttons.indices) { |
169 | 173 | 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 |
172 | 178 | else { |
| 179 | + mainVars += x |
173 | 180 | for (y3 <- 0 until y) |
174 | 181 | reduceUp(x, y, y3) |
175 | 182 |
|
176 | 183 | y += 1 |
177 | 184 | } |
178 | 185 | } |
| 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 | + } |
179 | 201 | } |
180 | 202 |
|
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 |
182 | 219 | } |
183 | 220 | } |
184 | 221 |
|
|
0 commit comments