Module aoc::year2016::day23

source ·
Expand description

§Safe Cracking

Like Day 12 this problem is all about reading code not writing code.

We could implement a brute force virtual machine without understanding the underlying code but it’s much more efficient to analyse the code instead.

The first thing we notice is that the following idiom is repeated several times:

    inc x
    dec y
    jnz y -2

This is equivalent to x += y only much less efficient. The tgl instruction eventually rewrites a jnz to cpy to allow the program loop to end.

Analysis shows that the code is calculating the factorial of a plus some constant offset. We can replace the entire code with a single multiplication. If we had emulated the raw instructions directly then it would have taken billions of iterations to get the answer.

Functions§

  • Extract the constant offset from the assembunny code.
  • 7! plus some constant.
  • 12! plus some constant.