summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-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);
 }
colspan='3' class='logmsg'> 2018-09-11Rewrite gfx.7 and render plaintext READMEJune McEnroe 2018-09-11Remove GAMES from BINSJune McEnroe 2018-09-11Rewrite bin.7 and render to plaintext READMEJune McEnroe 2018-09-11Add "blank" lines to man pagesJune McEnroe 2018-09-10Add mdoc syntax fileJune McEnroe 2018-09-08Fix Nm usage in multi-name man pagesJune McEnroe 2018-09-08Put real dates on man pagesJune McEnroe 2018-09-08Replace gfx README with REAMDE.7June McEnroe 2018-09-08Link gfx man pages in ~/.localJune McEnroe