summary refs log tree commit diff homepage
path: root/2019
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-11 18:00:44 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:26 -0500
commit91a98158b74c428b67af8a5c6764ae055ef60582 (patch)
tree657291a4cfd3208b8fae12aa0f2fe3ea73a186f5 /2019
parentSolve day 7 part 1 (diff)
downloadaoc-91a98158b74c428b67af8a5c6764ae055ef60582.tar.gz
aoc-91a98158b74c428b67af8a5c6764ae055ef60582.zip
Solve day 7 part 2
Yikes, I had gotten annoyed at this on day 7 because I didn't see what
was wrong. Turns out I was just missing amp[0].in = 0.
Diffstat (limited to '2019')
-rw-r--r--2019/day07.c63
1 files changed, 53 insertions, 10 deletions
diff --git a/2019/day07.c b/2019/day07.c
index d921683..c27aacd 100644
--- a/2019/day07.c
+++ b/2019/day07.c
@@ -2,11 +2,17 @@
 #include <stdlib.h>
 #include <string.h>
 
+enum {
+	Halt,
+	Input,
+	Output,
+};
+
 struct Intcode {
 	int ip;
 	int mode[3];
 	int mem[1024];
-	int in[2];
+	int in;
 	int out;
 };
 
@@ -14,20 +20,21 @@ static int *p(struct Intcode *m, int i) {
 	return (m->mode[i - 1] ? &m->mem[m->ip + i] : &m->mem[m->mem[m->ip + i]]);
 }
 
-static void run(struct Intcode *m) {
-	while (m->mem[m->ip] != 99) {
+static int run(struct Intcode *m) {
+	for (;;) {
 		m->mode[0] = m->mem[m->ip] / 100 % 10;
 		m->mode[1] = m->mem[m->ip] / 1000 % 10;
 		m->mode[2] = m->mem[m->ip] / 10000 % 10;
 		switch (m->mem[m->ip] % 100) {
 			break; case 1: *p(m, 3) = *p(m, 1) + *p(m, 2); m->ip += 4;
 			break; case 2: *p(m, 3) = *p(m, 1) * *p(m, 2); m->ip += 4;
-			break; case 3: *p(m, 1) = m->in[0]; m->in[0] = m->in[1]; m->ip += 2;
-			break; case 4: m->out = *p(m, 1); m->ip += 2;
+			break; case 3: *p(m, 1) = m->in; m->ip += 2; return Input;
+			break; case 4: m->out = *p(m, 1); m->ip += 2; return Output;
 			break; case 5: m->ip = (*p(m, 1) ? *p(m, 2) : m->ip + 3);
 			break; case 6: m->ip = (*p(m, 1) ? m->ip + 3 : *p(m, 2));
 			break; case 7: *p(m, 3) = (*p(m, 1) < *p(m, 2)); m->ip += 4;
 			break; case 8: *p(m, 3) = (*p(m, 1) == *p(m, 2)); m->ip += 4;
+			break; case 99: return Halt;
 			break; default: abort();
 		}
 	}
@@ -42,23 +49,59 @@ int main(void) {
 	for (int p = 0; p <= 044444; ++p) {
 		int x = p;
 		for (int i = 0; i < 5; ++i) {
-			amp[i].in[0] = x & 7;
+			amp[i].in = x & 7;
 			x >>= 3;
 		}
 		for (int i = 0; i < 5; ++i) {
-			if (amp[i].in[0] > 4) goto skip;
+			if (amp[i].in > 4) goto skip;
 			for (int j = 0; j < 5; ++j) {
-				if (i != j && amp[i].in[0] == amp[j].in[0]) goto skip;
+				if (i != j && amp[i].in == amp[j].in) goto skip;
 			}
 		}
 		for (int i = 0; i < 5; ++i) {
 			amp[i].ip = 0;
 			memcpy(amp[i].mem, mem, sizeof(amp[i].mem));
-			amp[i].in[1] = (i ? amp[i - 1].out : 0);
-			run(&amp[i]);
+			while (run(&amp[i]) != Input);
+			amp[i].in = (i ? amp[i - 1].out : 0);
+			while (run(&amp[i]) != Halt);
 		}
 		if (amp[4].out > max) max = amp[4].out;
 skip:;
 	}
 	printf("%d\n", max);
+
+	max = 0;
+	for (int p = 0; p <= 044444; ++p) {
+		int x = p;
+		for (int i = 0; i < 5; ++i) {
+			amp[i].in = 5 + (x & 7);
+			amp[i].out = 0;
+			x >>= 3;
+		}
+		for (int i = 0; i < 5; ++i) {
+			if (amp[i].in > 9) goto skip2;
+			for (int j = 0; j < 5; ++j) {
+				if (i != j && amp[i].in == amp[j].in) goto skip2;
+			}
+		}
+		for (int i = 0; i < 5; ++i) {
+			amp[i].ip = 0;
+			memcpy(amp[i].mem, mem, sizeof(amp[i].mem));
+			run(&amp[i]);
+		}
+		amp[0].in = 0;
+		for (int i = 0;;) {
+			int x = run(&amp[i]);
+			if (x == Output) {
+				int out = amp[i].out;
+				i = (i + 1) % 5;
+				amp[i].in = out;
+			} else if (x == Halt) {
+				break;
+			}
+		}
+		if (amp[4].out > max) max = amp[4].out;
+skip2:;
+	}
+	printf("%d\n", max);
 }