summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-08 00:16:17 -0500
committerJune McEnroe <june@causal.agency>2020-12-08 00:16:17 -0500
commit522489f0ce2457217032c0a3f42bea292f045072 (patch)
tree1f9c27f98e5d2cf9a94435a64c2139aa819a0319
parentSolve day 8 part 1 (diff)
downloadaoc-522489f0ce2457217032c0a3f42bea292f045072.tar.gz
aoc-522489f0ce2457217032c0a3f42bea292f045072.zip
Solve day 8 part 2
-rw-r--r--2020/day08.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/2020/day08.c b/2020/day08.c
index 87b4585..7afd969 100644
--- a/2020/day08.c
+++ b/2020/day08.c
@@ -5,6 +5,7 @@ static struct Ins {
 	char op[4];
 	int arg;
 } prog[1024];
+static int len;
 static int acc;
 static int pc;
 static void step(void) {
@@ -16,14 +17,31 @@ static void step(void) {
 	}
 	pc++;
 }
-int main(void) {
-	int i = 0;
-	while (EOF != scanf("%s %d\n", prog[i].op, &prog[i].arg)) {
-		i++;
-	}
+static int terminates(void) {
+	acc = 0;
+	pc = 0;
 	int ran[1024] = {0};
-	while (!ran[pc]++) {
+	while (!ran[pc]++ && pc < len) {
 		step();
 	}
+	return pc == len;
+}
+int main(void) {
+	while (EOF != scanf("%s %d\n", prog[len].op, &prog[len].arg)) {
+		len++;
+	}
+	terminates();
+	printf("%d\n", acc);
+	for (int i = 0; i < len; ++i) {
+		if (!strcmp(prog[i].op, "jmp")) {
+			strcpy(prog[i].op, "nop");
+			if (terminates()) break;
+			strcpy(prog[i].op, "jmp");
+		} else if (!strcmp(prog[i].op, "nop")) {
+			strcpy(prog[i].op, "jmp");
+			if (terminates()) break;
+			strcpy(prog[i].op, "nop");
+		}
+	}
 	printf("%d\n", acc);
 }