summary refs log tree commit diff homepage
path: root/2018/day19.c
diff options
context:
space:
mode:
Diffstat (limited to '2018/day19.c')
-rw-r--r--2018/day19.c78
1 files changed, 69 insertions, 9 deletions
diff --git a/2018/day19.c b/2018/day19.c
index 5802ffa..eed25e1 100644
--- a/2018/day19.c
+++ b/2018/day19.c
@@ -88,25 +88,85 @@ int main(void) {
 
 	uint len = 0;
 	struct Ins {
-		char op[4];
-		uint a, b, c;
+		uint op, a, b, c;
 	} prog[50];
 	while (!feof(stdin)) {
+		char code[4];
 		scanf(
 			"%4c %u %u %u\n",
-			prog[len].op, &prog[len].a, &prog[len].b, &prog[len].c
+			code, &prog[len].a, &prog[len].b, &prog[len].c
 		);
+		for (uint i = 0; i < 16; ++i) {
+			if (strncmp(code, Ops[i].name, 4)) continue;
+			prog[len].op = i;
+			break;
+		}
 		len++;
 	}
 
 	while (cpu.r[cpu.ip] < len) {
 		struct Ins ins = prog[cpu.r[cpu.ip]];
-		for (uint i = 0; i < 16; ++i) {
-			if (strncmp(ins.op, Ops[i].name, 4)) continue;
-			cpu = Ops[i].fn(cpu, ins.a, ins.b, ins.c);
-			cpu.r[cpu.ip]++;
-			break;
-		}
+		cpu = Ops[ins.op].fn(cpu, ins.a, ins.b, ins.c);
+		cpu.r[cpu.ip]++;
 	}
 	printf("%u\n", cpu.r[0]);
+
+	/*
+	00:	addi	ip	16	ip	goto 17
+	01:	seti	1	_	r5	r5 = 1
+	02:	seti	1	_	r3	r3 = 1
+	03:	mulr	r5	r3	r2	r2 = r5 * r3
+	04:	eqrr	r2	r4	r2	r2 = (r2 == r4)
+	05:	addr	r2	ip	ip	if r2 goto 7 else goto 6
+	06:	addi	ip	1	ip	goto 8
+	07:	addr	r5	r0	r0	r0 += r5
+	08:	addi	r3	1	r3	r3 += 1
+	09:	gtrr	r3	r4	r2	r2 = (r3 > r4)
+	10:	addr	ip	r2	ip	if r2 goto 12 else goto 11
+	11:	seti	2	_	ip	goto 3
+	12:	addi	r5	1	r5	r5 += 1
+	13:	gtrr	r5	r4	r2	r2 = (r5 > r4)
+	14:	addr	r2	ip	ip	if r2 goto 16 else goto 15
+	15:	seti	1	_	ip	goto 2
+	16:	mulr	ip	ip	ip	halt
+	17:	addi	r4	2	r4	r4 += 2
+	18:	mulr	r4	r4	r4	r4 *= r4
+	19:	mulr	ip	r4	r4	r4 *= 19
+	20:	muli	r4	11	r4	r4 *= 11
+	21:	addi	r2	5	r2	r2 += 5
+	22:	mulr	r2	ip	r2	r2 *= 22
+	23:	addi	r2	12	r2	r2 += 12
+	24:	addr	r4	r2	r4	r4 += r2
+	25:	addr	ip	r0	ip	if r0 goto 27 else goto 26
+	26:	seti	0	_	ip	goto 1
+	27:	setr	ip	_	r2	r2 = 27
+	28:	mulr	r2	ip	r2	r2 *= 28
+	29:	addr	ip	r2	r2	r2 += 29
+	30:	mulr	ip	r2	r2	r2 *= 30
+	31:	muli	r2	14	r2	r2 *= 14
+	32:	mulr	r2	ip	r2	r2 *= 32
+	33:	addr	r4	r2	r4	r4 += r2
+	34:	seti	0	_	r0	r0 = 0
+	35:	seti	0	_	ip	goto 1
+	*/
+
+#if 0
+	uint sum = 0;
+	uint big = 10551358;
+	for (uint a = 1; a <= big; a++) {
+		for (uint b = 1; b <= big; b++) {
+			if (a * b == big) sum += a;
+		}
+	}
+	printf("%u\n", r0);
+#endif
+
+	uint big = 10551358;
+	uint sum = 1 + big;
+	for (uint f = 2; big > 1; ++f) {
+		if (big % f) continue;
+		sum += f;
+		big /= f;
+	}
+	printf("%u\n", sum);
 }