Skip to content

Commit

Permalink
Generalize bxl literals in 2024 day 17 part 2 Z3 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 17, 2024
1 parent 917aa1c commit 6260cd1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main/scala/eu/sim642/adventofcode2024/Day17.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ object Day17 {

object Z3Part2Solution extends Part2Solution {
override def findQuineA(input: Input): Long = {
val Seq(2,4,1,bxl1,7,5,1,bxl2,4,5,0,3,5,5,3,0) = input.program

val ctx = new Context(Map("model" -> "true").asJava)
import ctx._
val s = mkSolver()
Expand All @@ -114,16 +116,17 @@ object Day17 {

for ((instruction, i) <- input.program.zipWithIndex) {
val a = mkBVLSHR(initialA, mkBV(i * 3, bits))
var b = mkBVAND(a, mkBV(7, bits))
b = mkBVXOR(b, mkBV(1, bits))
var b = mkBVAND(a, mkBV(0b111, bits))
b = mkBVXOR(b, mkBV(bxl1, bits))
val c = mkBVLSHR(a, b)
b = mkBVXOR(b, mkBV(5, bits))
b = mkBVXOR(b, mkBV(bxl2, bits))
b = mkBVXOR(b, c)
val out = mkBVAND(b, mkBV(7, bits))
val out = mkBVAND(b, mkBV(0b111, bits))
s.add(mkEq(out, mkBV(instruction, bits)))
}

assert(s.check() == Status.SATISFIABLE)
// TODO: minimize
s.getModel.evaluate(initialA, false).toString.toLong
}
}
Expand Down

0 comments on commit 6260cd1

Please sign in to comment.