diff options
author | June McEnroe <june@causal.agency> | 2019-12-07 15:46:42 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2019-12-07 15:46:42 -0500 |
commit | 85d874b1a28bf849f4d64d94a0a807940f443618 (patch) | |
tree | 538b230b7be7a53624128fb69ce0612bbcd1736f /2019/day07.c | |
parent | Solve day 6 part 2 (diff) | |
download | aoc-85d874b1a28bf849f4d64d94a0a807940f443618.tar.gz aoc-85d874b1a28bf849f4d64d94a0a807940f443618.zip |
Solve day 7 part 1
Diffstat (limited to '')
-rw-r--r-- | 2019/day07.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/2019/day07.c b/2019/day07.c new file mode 100644 index 0000000..d921683 --- /dev/null +++ b/2019/day07.c @@ -0,0 +1,64 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct Intcode { + int ip; + int mode[3]; + int mem[1024]; + int in[2]; + int out; +}; + +static int *p(struct Intcode *m, int i) { + return (m->mode[i - 1] ? &m->mem[m->ip + i] : &m->mem[m->mem[m->ip + i]]); +} + +static void run(struct Intcode *m) { + while (m->mem[m->ip] != 99) { + m->mode[0] = m->mem[m->ip] / 100 % 10; + m->mode[1] = m->mem[m->ip] / 1000 % 10; + m->mode[2] = m->mem[m->ip] / 10000 % 10; + switch (m->mem[m->ip] % 100) { + break; case 1: *p(m, 3) = *p(m, 1) + *p(m, 2); m->ip += 4; + break; case 2: *p(m, 3) = *p(m, 1) * *p(m, 2); m->ip += 4; + break; case 3: *p(m, 1) = m->in[0]; m->in[0] = m->in[1]; m->ip += 2; + break; case 4: m->out = *p(m, 1); m->ip += 2; + break; case 5: m->ip = (*p(m, 1) ? *p(m, 2) : m->ip + 3); + break; case 6: m->ip = (*p(m, 1) ? m->ip + 3 : *p(m, 2)); + break; case 7: *p(m, 3) = (*p(m, 1) < *p(m, 2)); m->ip += 4; + break; case 8: *p(m, 3) = (*p(m, 1) == *p(m, 2)); m->ip += 4; + break; default: abort(); + } + } +} + +int main(void) { + int mem[1024] = {0}; + for (int i = 0; EOF != scanf("%d,", &mem[i]); ++i); + + struct Intcode amp[5] = {0}; + int max = 0; + for (int p = 0; p <= 044444; ++p) { + int x = p; + for (int i = 0; i < 5; ++i) { + amp[i].in[0] = x & 7; + x >>= 3; + } + for (int i = 0; i < 5; ++i) { + if (amp[i].in[0] > 4) goto skip; + for (int j = 0; j < 5; ++j) { + if (i != j && amp[i].in[0] == amp[j].in[0]) goto skip; + } + } + for (int i = 0; i < 5; ++i) { + amp[i].ip = 0; + memcpy(amp[i].mem, mem, sizeof(amp[i].mem)); + amp[i].in[1] = (i ? amp[i - 1].out : 0); + run(&[i]); + } + if (amp[4].out > max) max = amp[4].out; +skip:; + } + printf("%d\n", max); +} |