summary refs log tree commit diff homepage
path: root/2019/day05.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-06 03:10:23 -0500
committerJune McEnroe <june@causal.agency>2019-12-06 03:10:23 -0500
commit3c641af87a0e272c03cc0f273055124164d7d360 (patch)
treebe4afb118b59f4f53470d69ce6d1a40384735e72 /2019/day05.c
parentAdd alternate shell solution for day 4 (diff)
downloadaoc-3c641af87a0e272c03cc0f273055124164d7d360.tar.gz
aoc-3c641af87a0e272c03cc0f273055124164d7d360.zip
Solve day 5 part 1
Diffstat (limited to '')
-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();
+}