summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-02 00:23:37 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:26 -0500
commitaa844a7263afa1f34e466bb9ee71ff67fdae2858 (patch)
treedda0bfea11f58ea2ceae5b57113554540de587bd
parentSolve day 2 part 1 (diff)
downloadaoc-aa844a7263afa1f34e466bb9ee71ff67fdae2858.tar.gz
aoc-aa844a7263afa1f34e466bb9ee71ff67fdae2858.zip
Solve day 2 part 2
-rw-r--r--2019/day02.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/2019/day02.c b/2019/day02.c
index 411762f..c574b57 100644
--- a/2019/day02.c
+++ b/2019/day02.c
@@ -1,10 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
-int main(void) {
-	int v[1024] = {0};
-	for (int i = 0; EOF != scanf("%d,", &v[i]); ++i);
-	v[1] = 12;
-	v[2] = 2;
+#include <string.h>
+static void run(int *v) {
 	for (int i = 0; v[i] != 99; i += 4) {
 		switch (v[i]) {
 			break; case 1: v[v[i + 3]] = v[v[i + 1]] + v[v[i + 2]];
@@ -12,5 +9,25 @@ int main(void) {
 			break; default: abort();
 		}
 	}
+}
+int main(void) {
+	int a[1024] = {0};
+	for (int i = 0; EOF != scanf("%d,", &a[i]); ++i);
+	int v[1024];
+	memcpy(v, a, sizeof(v));
+	v[1] = 12;
+	v[2] = 2;
+	run(v);
 	printf("%d\n", v[0]);
+	for (int noun = 0; noun < 100; ++noun) {
+		for (int verb = 0; verb < 100; ++verb) {
+			memcpy(v, a, sizeof(v));
+			v[1] = noun;
+			v[2] = verb;
+			run(v);
+			if (v[0] != 19690720) continue;
+			printf("%d\n", 100 * noun + verb);
+			return 0;
+		}
+	}
 }