summary refs log tree commit diff homepage
path: root/2020/day08.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-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);
 }