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
commit99c7c35f91a921138a330f9c2c0da43b16b408dc (patch)
tree6721d6594927e1e92ae898e960925825fb5fb3d5
parentSolve day 8 part 1 (diff)
downloadaoc-99c7c35f91a921138a330f9c2c0da43b16b408dc.tar.gz
aoc-99c7c35f91a921138a330f9c2c0da43b16b408dc.zip
Solve day 8 part 2
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);
 }
ow=1'>Check that password is hashedJune McEnroe 2019-11-08Avoid calling getopt_long again after it returns -1June McEnroe 2019-11-08Only change AWAY status for registered clientsJune McEnroe 2019-11-07Just write the example normallyJune McEnroe 2019-11-07Include path in readlinkat errorJune McEnroe 2019-11-07Call clientConsume before clientRecvJune McEnroe 2019-11-06Use -l:filename in Linux.mkJune McEnroe 2019-11-06Fix compat.h for #defined strlcpyJune McEnroe 2019-11-06Allow unsetting LIBRESSL_PREFIXJune McEnroe 2019-11-06Document calico service configurationJune McEnroe 2019-11-06Document SASL EXTERNAL configuration in more detailJune McEnroe 2019-11-06Document pounce service configurationJune McEnroe 2019-11-06Mention Darwin and GNU/Linux in READMEJune McEnroe 2019-11-06Assume LibreSSL from brew on DarwinJune McEnroe 2019-11-06Remove -DNO_EXPLICIT_BZERO from Darwin.mkJune McEnroe 2019-11-06Don't install rc scripts or dirs on LinuxJune McEnroe