diff options
author | June McEnroe <june@causal.agency> | 2020-12-08 00:16:17 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-12-08 00:16:17 -0500 |
commit | 522489f0ce2457217032c0a3f42bea292f045072 (patch) | |
tree | 1f9c27f98e5d2cf9a94435a64c2139aa819a0319 | |
parent | Solve day 8 part 1 (diff) | |
download | aoc-522489f0ce2457217032c0a3f42bea292f045072.tar.gz aoc-522489f0ce2457217032c0a3f42bea292f045072.zip |
Solve day 8 part 2
-rw-r--r-- | 2020/day08.c | 30 |
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); } |