Skip to content

Instantly share code, notes, and snippets.

@asciipip
Last active December 23, 2019 02:32
Show Gist options
  • Save asciipip/ff10c7aa041d610a4b5b71ee2c91709b to your computer and use it in GitHub Desktop.
Save asciipip/ff10c7aa041d610a4b5b71ee2c91709b to your computer and use it in GitHub Desktop.
Advent of Code 2019 Day 2 Intcode disassembly and analysis
"1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,6,19,1,19,5,23,2,13,23,27,1,10,27,31,2,6,31,35,1,9,35,39,2,10,39,43,1,43,9,47,1,47,9,51,2,10,51,55,1,55,9,59,1,59,5,63,1,63,6,67,2,6,67,71,2,10,71,75,1,75,5,79,1,9,79,83,2,83,10,87,1,87,6,91,1,13,91,95,2,10,95,99,1,99,6,103,2,13,103,107,1,107,2,111,1,111,9,0,99,2,14,0,0"
0: ADD( 1) 0 0 3
4: ADD( 1) 1 2 3
8: ADD( 1) 3 4 3
12: ADD( 1) 5 0 3
16: MUL( 2) 1 6 19
20: ADD( 1) 19 5 23
24: MUL( 2) 13 23 27
28: ADD( 1) 10 27 31
32: MUL( 2) 6 31 35
36: ADD( 1) 9 35 39
40: MUL( 2) 10 39 43
44: ADD( 1) 43 9 47
48: ADD( 1) 47 9 51
52: MUL( 2) 10 51 55
56: ADD( 1) 55 9 59
60: ADD( 1) 59 5 63
64: ADD( 1) 63 6 67
68: MUL( 2) 6 67 71
72: MUL( 2) 10 71 75
76: ADD( 1) 75 5 79
80: ADD( 1) 9 79 83
84: MUL( 2) 83 10 87
88: ADD( 1) 87 6 91
92: ADD( 1) 13 91 95
96: MUL( 2) 10 95 99
100: ADD( 1) 99 6 103
104: MUL( 2) 13 103 107
108: ADD( 1) 107 2 111
112: ADD( 1) 111 9 0
116: HLT(99)
117: MUL( 2) 14 0 0
NOTE: I'll call memory cell 0 "output", cell 1 "noun", and cell 2 "verb".
Everything else is cNN (e.g. c3 or c107), based on their memory location.
NOTE 2: When an opcode parameter references a value that hasn't changed from its
original value, I'll treat it as a constant.
c3 = 1 + 1
c3 = noun + verb
c3 = c3 + 1
c3 = 1 + 1
c19 = noun * 2
c23 = c19 + 1
c27 = 5 * c23
c31 = 4 + c27
c35 = 2 * c31
c39 = 3 + c35
c43 = 4 * c39
c47 = c43 + 3
c51 = c47 + 3
c55 = 4 * c51
c59 = c55 + 3
c63 = c59 + 1
c67 = c63 + 2
c71 = 2 * c67
c75 = 4 * c71
c79 = c75 + 1
c83 = 3 + c79
c87 = c83 * 4
c91 = c87 + 2
c95 = 5 + c91
c99 = 4 * c95
c103 = c99 + 2
c107 = 5 * c103
c111 = c107 + verb
output = c111 + 3
So it appears that the first several opcodes are dummies. They exist to make
room for the inputs and output (memory cells 0-3) and for several numeric
constants (cells 4-15). The calculation starts with the opcode at cell 16.
If we expand out the calculation, we get:
5 * (4 * (5 + (3 + 4 * (2 * (4 * (4 * (3 + 2 * (4 + 5 * (noun * 2 + 1))) + 3 + 3) + 3 + 1 + 2)) + 1) * 4 + 2) + 2) + verb + 3
Simplified, that turns into:
noun * 204800 + 234713 + verb
Since we know the output should be 19690720, that turns into:
204800 * noun + verb + 234713 = 19690720
Simplify:
204800 * noun + verb = 19456007
Move things around:
noun = 19456007 / 204800 - verb / 204800
Divide:
noun = 95 + 7 / 204800 - verb / 204800
From this, it's pretty clear that the noun is 95 and the verb is 7.
@estysdesu
Copy link

Pretty neat analysis. Was wondering if it was possible to analyze and mathematically determine day 2 part 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment