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 analyze the code instead.
The first thing we notice is that the following idiom is repeated several times:
inc x
dec y
jnz y -2This 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.