summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--2019/day05.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/2019/day05.c b/2019/day05.c
new file mode 100644
index 0000000..3b38124
--- /dev/null
+++ b/2019/day05.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+static int input(void) {
+	return 1;
+}
+
+static void output(int x) {
+	printf("%d\n", x);
+}
+
+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(void) {
+	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: output(*p(m1, 1)); ip += 2;
+			break; default: abort();
+		}
+	}
+}
+
+int main(void) {
+	for (int i = 0; EOF != scanf("%d,", &mem[i]); ++i);
+	run();
+}