summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-06 03:10:23 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:26 -0500
commit2545a97506fe589917491ff9dfe3bddc0d2cde0f (patch)
tree7446e337dfbc0a5d4b3781a129daea3df6fb030f
parentAdd alternate shell solution for day 4 (diff)
downloadaoc-2545a97506fe589917491ff9dfe3bddc0d2cde0f.tar.gz
aoc-2545a97506fe589917491ff9dfe3bddc0d2cde0f.zip
Solve day 5 part 1
-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();
+}