Go With The Flow


Fork me on GitHub
2018-12-19

Day 19: Go With The Flow

Description:
--- Day 19: Go With The Flow ---

With the Elves well on their way constructing the North Pole base, you turn your attention back to understanding the inner workings of programming the device.

You can't help but notice that the device's opcodes don't contain any flow control like jump instructions. The device's manual goes on to explain:

"In programs where flow control is required, the instruction pointer can be bound to a register so that it can be manipulated directly. This way, setr/seti can function as absolute jumps, addr/addi can function as relative jumps, and other opcodes can cause truly fascinating effects."

This mechanism is achieved through a declaration like #ip 1, which would modify register 1 so that accesses to it let the program indirectly access the instruction pointer itself. To compensate for this kind of binding, there are now six registers (numbered 0 through 5); the five not bound to the instruction pointer behave as normal. Otherwise, the same rules apply as the last time you worked with this device.

When the instruction pointer is bound to a register, its value is written to that register just before each instruction is executed, and the value of that register is written back to the instruction pointer immediately after each instruction finishes execution. Afterward, move to the next instruction by adding one to the instruction pointer, even if the value in the instruction pointer was just updated by an instruction. (Because of this, instructions must effectively set the instruction pointer to the instruction before the one they want executed next.)

The instruction pointer is 0 during the first instruction, 1 during the second, and so on. If the instruction pointer ever causes the device to attempt to load an instruction outside the instructions defined in the program, the program instead immediately halts. The instruction pointer starts at 0.

It turns out that this new information is already proving useful: the CPU in the device is not very powerful, and a background process is occupying most of its time. You dump the background process' declarations and instructions to a file (your puzzle input), making sure to use the names of the opcodes rather than the numbers.

For example, suppose you have the following program:

#ip 0
seti 5 0 1
seti 6 0 2
addi 0 1 0
addr 1 2 3
setr 1 0 0
seti 8 0 4
seti 9 0 5

When executed, the following instructions are executed. Each line contains the value of the instruction pointer at the time the instruction started, the values of the six registers before executing the instructions (in square brackets), the instruction itself, and the values of the six registers after executing the instruction (also in square brackets).

ip=0 [0, 0, 0, 0, 0, 0] seti 5 0 1 [0, 5, 0, 0, 0, 0]
ip=1 [1, 5, 0, 0, 0, 0] seti 6 0 2 [1, 5, 6, 0, 0, 0]
ip=2 [2, 5, 6, 0, 0, 0] addi 0 1 0 [3, 5, 6, 0, 0, 0]
ip=4 [4, 5, 6, 0, 0, 0] setr 1 0 0 [5, 5, 6, 0, 0, 0]
ip=6 [6, 5, 6, 0, 0, 0] seti 9 0 5 [6, 5, 6, 0, 0, 9]

In detail, when running this program, the following events occur:

The first line (#ip 0) indicates that the instruction pointer should be bound to register 0 in this program. This is not an instruction, and so the value of the instruction pointer does not change during the processing of this line.
The instruction pointer contains 0, and so the first instruction is executed (seti 5 0 1). It updates register 0 to the current instruction pointer value (0), sets register 1 to 5, sets the instruction pointer to the value of register 0 (which has no effect, as the instruction did not modify register 0), and then adds one to the instruction pointer.
The instruction pointer contains 1, and so the second instruction, seti 6 0 2, is executed. This is very similar to the instruction before it: 6 is stored in register 2, and the instruction pointer is left with the value 2.
The instruction pointer is 2, which points at the instruction addi 0 1 0. This is like a relative jump: the value of the instruction pointer, 2, is loaded into register 0. Then, addi finds the result of adding the value in register 0 and the value 1, storing the result, 3, back in register 0. Register 0 is then copied back to the instruction pointer, which will cause it to end up 1 larger than it would have otherwise and skip the next instruction (addr 1 2 3) entirely. Finally, 1 is added to the instruction pointer.
The instruction pointer is 4, so the instruction setr 1 0 0 is run. This is like an absolute jump: it copies the value contained in register 1, 5, into register 0, which causes it to end up in the instruction pointer. The instruction pointer is then incremented, leaving it at 6.
The instruction pointer is 6, so the instruction seti 9 0 5 stores 9 into register 5. The instruction pointer is incremented, causing it to point outside the program, and so the program ends.

What value is left in register 0 when the background process halts?

--- Part Two ---

A new background process immediately spins up in its place. It appears identical, but on closer inspection, you notice that this time, register 0 started with the value 1.

What value is left in register 0 when this new background process halts?

Input:
#ip 1
addi 1 16 1
seti 1 5 5
seti 1 2 3
mulr 5 3 2
eqrr 2 4 2
addr 2 1 1
addi 1 1 1
addr 5 0 0
addi 3 1 3
gtrr 3 4 2
addr 1 2 1
seti 2 6 1
addi 5 1 5
gtrr 5 4 2
addr 2 1 1
seti 1 8 1
mulr 1 1 1
addi 4 2 4
mulr 4 4 4
mulr 1 4 4
muli 4 11 4
addi 2 5 2
mulr 2 1 2
addi 2 12 2
addr 4 2 4
addr 1 0 1
seti 0 4 1
setr 1 4 2
mulr 2 1 2
addr 1 2 2
mulr 1 2 2
muli 2 14 2
mulr 2 1 2
addr 4 2 4
seti 0 3 0
seti 0 7 1

Part 1:
class Command { public String Name; public (string, Action<int[], Command>) Op; public int[] Args; public int Dst => Args[2]; public int Arg1 => Args[0]; public int Arg2 => Args[1]; }

(string, Action<int[], Command>)[] Operations = new(string, Action<int[], Command>)[]
{
	("addr", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] + reg[cmd.Arg2];        } ),
	("addi", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] +     cmd.Arg2;         } ),
	("mulr", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] * reg[cmd.Arg2];        } ),
	("muli", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] *     cmd.Arg2;         } ),
	("banr", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] & reg[cmd.Arg2];        } ),
	("bani", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] &     cmd.Arg2;         } ),
	("borr", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] | reg[cmd.Arg2];        } ),
	("bori", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] |     cmd.Arg2;         } ),
	("setr", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1];                        } ),
	("seti", (reg, cmd) => { reg[cmd.Dst] =     cmd.Arg1;                         } ),
	("gtir", (reg, cmd) => { reg[cmd.Dst] =     cmd.Arg1  >  reg[cmd.Arg2] ? 1:0; } ),
	("gtri", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] >      cmd.Arg2  ? 1:0; } ),
	("gtrr", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] >  reg[cmd.Arg2] ? 1:0; } ),
	("eqir", (reg, cmd) => { reg[cmd.Dst] =     cmd.Arg1  == reg[cmd.Arg2] ? 1:0; } ),
	("eqri", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] ==     cmd.Arg2  ? 1:0; } ),
	("eqrr", (reg, cmd) => { reg[cmd.Dst] = reg[cmd.Arg1] == reg[cmd.Arg2] ? 1:0; } ),
};

void Main()
{
	(var commands, var ipidx) = Load(File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"19_input.txt")));

	int[] data = new int[255];
	for (int gen=0;;gen++)
	{
		if (data[ipidx]<0 || data[ipidx]>=commands.Count) break;
		
		var cmd = commands[data[ipidx]];
		
		cmd.Op.Item2(data, cmd);
		//string.Join(" ", data.Take(6)).Dump();
		data[ipidx]++;
	}
	
	data[0].Dump();
}

(List<Command>, int) Load(string[] input)
{
	return
	(
		input
			.Skip(1)
			.Where(i => !string.IsNullOrWhiteSpace(i))
			.Select(i => new Command { Name = i.Split(' ')[0], Op=Operations.Single(o => o.Item1.ToLower() == i.Split(' ')[0].ToLower()), Args = i.Split(' ').Skip(1).Select(int.Parse).ToArray() })
			.ToList(),
		int.Parse(input[0].Split(' ')[1])
	);
}
Result: 1440

Part 2:
void Main()
{
	var b = 10551358;
	var a = 0;
	for (int i = 1; i <= b; i++) if (b % i == 0)a+=i;
	a.Dump();
}

/*
 [1-ORIGINAL]           [2-SYNTAX]           [3-RESOLVE]         [4-SIMPLIFY]

#ip 1
00: addi 1 16 1           [1]=[1]+16            JMP 17             JMP 17                         
01: seti 1 5 5            [5]=1                                1:  E := 1                         
02: seti 1 2 3            [3]=1                                2:  C := 1                         
03: mulr 5 3 2            [2]=[3]*[5]                          3:  B := C*E                       
04: eqrr 2 4 2            [2]=([2]==[4])                           if (B = D) A += E              
05: addr 2 1 1            [1]=[2]+[1]           JMP 6+[2]                                         
06: addi 1 1 1            [1]++                 JMP 8                                             
07: addr 5 0 0            [0]=[5]+[0]                                                             
08: addi 3 1 3            [3]++                                    C++                            
09: gtrr 3 4 2            [2]=([3]>[4])                            if (C<=D) JUMP 3               
10: addr 1 2 1            [1]=[1]+[2]           JMP 11+[2]                                        
11: seti 2 6 1            [1]=2                 JMP 3                                             
12: addi 5 1 5            [5]++                                    E++                            
13: gtrr 5 4 2            [2]=([5]>[4])                            if (E<=D) JUMP 2 else EXIT     
14: addr 2 1 1            [1]=[1]+[2]           JMP 15+[2]                                        
15: seti 1 8 1            [1]=1                 JMP 2                                             
16: mulr 1 1 1            [1]=[1]*[1]           EXIT                                              
17: addi 4 2 4            [4]+=2                              17:  D+=2                          |
18: mulr 4 4 4            [4]=[4]*[4]                              D=D*D                         |
19: mulr 1 4 4            [4]=[4]*[1]           [4]=[4]*19         D*=19                         |
20: muli 4 11 4           [4]=[4]*[1]           [4]=[4]*11         D*=11                         |
21: addi 2 5 2            [2]=[2]+5                                B+=5                          |
22: mulr 2 1 2            [2]=[2]*[1]           [2]=[2]*22         B*=22                         |
23: addi 2 12 2           [2]=[2]+12                               B+=12                         |B := 122
24: addr 4 2 4            [4]=[4]+[2]                              D+=B                          |D := 958
25: addr 1 0 1            [1]=[1]+[0]           JMP 26+[0]         if (A = 0) JUMP 1              
26: seti 0 4 1            [1]=0                 JMP 1                                             
27: setr 1 4 2            [2]=[1]               [2]=[4]+27         B=27                          |
28: mulr 2 1 2            [2]=[2]*[1]           [2]=[2]*28         B=B*28                        |
29: addr 1 2 2            [2]=[2]+[1]           [2]=[2]+29         B=B+29                        |
30: mulr 1 2 2            [2]=[2]*[1]           [2]=[2]*30         B=B*30                        |
31: muli 2 14 2           [2]=[2]*14                               B=B*14                        |
32: mulr 2 1 2            [2]=[2]*[1]           [2]=[2]*32         B=B*32                        |B := 10550400
33: addr 4 2 4            [4]=[2]+[4]                              D=B+D                         |D := 10551358
34: seti 0 3 0            [0]=0                                    A=0                            
35: seti 0 7 1            [1]=0                 JMP 1              JUMP 1                         




[5-SHRINK]

     JMP 17
 1:  E := 1
 2:  C := 1
 3:  B := C*E
     if (B = D) A += E
     C++
     if (C<=D) JUMP 3
     E++
     if (E<=D) JUMP 2 else EXIT
17:  B = 122
     D = 958
     if (A = 0) JUMP 1
     B = 10550400
     D = 10551358
     A=0
     JUMP 1




[6-REORDER]

if (A=0) 
    B = 122; D = 958
else
    B = 10550400; D = 10551358;

A = 0

    E=1
S1: C=1
S2:     B=C*E
        if (B = D) A += E
        C++
        if (C<=D) JUMP S2
        E++
    if (E<=D)
        JUMP S1
    else
        EXIT




[7-LANGUAGE]

if (A=0) B = 122;      D = 958;
else     B = 10550400; D = 10551358;

A = 0
E = 1

do
{
	C=1
	do
	{
		B=C*E
		if (B == D) A += E
		C++
	}
	while (C <= D)
	E++
}
while (E <= D)




[8-SUGAR]

if (A=0) B = 122;      D = 958;
else     B = 10550400; D = 10551358;

A = 0
E = 1

for(E=1; E<=D; E++)
{
	for(C=1; C<=D; C++)
	{
		if (C*E == D) A += E
	}
}




[9-EXPLANATION]

THE SUM OF ALL FACTORS OF 10551358

*/
Result: 15827040


made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript