summary refs log tree commit diff homepage
path: root/2019/day05.c
blob: ec8c471cfdd23c857a4fa7c89345050a800e207b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int ip;
static int mem[1024];

static int *p(int mode, int param) {
	return (mode ? &mem[ip + param] : &mem[mem[ip + param]]);
}

static void run(int input) {
	while (mem[ip] != 99) {
		int op = mem[ip] % 100;
		int m1 = mem[ip] / 100 % 10;
		int m2 = mem[ip] / 1000 % 10;
		int m3 = mem[ip] / 10000 % 10;
		switch (op) {
			break; case 1: *p(m3, 3) = *p(m1, 1) + *p(m2, 2); ip += 4;
			break; case 2: *p(m3, 3) = *p(m1, 1) * *p(m2, 2); ip += 4;
			break; case 3: *p(m1, 1) = input; ip += 2;
			break; case 4: printf("%d\n", *p(m1, 1)); ip += 2;
			break; case 5: ip = (*p(m1, 1) ? *p(m2, 2) : ip + 3);
			break; case 6: ip = (*p(m1, 1) ? ip + 3 : *p(m2, 2));
			break; case 7: *p(m3, 3) = (*p(m1, 1) < *p(m2, 2)); ip += 4;
			break; case 8: *p(m3, 3) = (*p(m1, 1) == *p(m2, 2)); ip += 4;
			break; default: abort();
		}
	}
}

int main(void) {
	int a[1024];
	for (int i = 0; EOF != scanf("%d,", &a[i]); ++i);
	memcpy(mem, a, sizeof(mem));
	run(1);
	memcpy(mem, a, sizeof(mem));
	ip = 0;
	run(5);
}