diff options
author | June McEnroe <june@causal.agency> | 2018-12-17 16:14:16 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2018-12-17 16:14:16 -0500 |
commit | 4b60c93536d20a68ca2be5d0fa57b9afc19fa064 (patch) | |
tree | 72c3e6ddd50f3917851502d55e0b4b986db5c722 /2018 | |
parent | Solve day 16 part 1 (diff) | |
download | aoc-4b60c93536d20a68ca2be5d0fa57b9afc19fa064.tar.gz aoc-4b60c93536d20a68ca2be5d0fa57b9afc19fa064.zip |
Solve day 16 part 2
Diffstat (limited to '')
-rw-r--r-- | 2018/day16.c | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/2018/day16.c b/2018/day16.c index a1a72c4..c8f5cde 100644 --- a/2018/day16.c +++ b/2018/day16.c @@ -88,19 +88,10 @@ static const struct { { "eqrr", eqrr }, }; -static uint card(Set set) { - uint pop = 0; - while (set) { - if (set & 1) pop++; - set >>= 1; - } - return pop; -} - int main(void) { - Set opcodes[16]; + Set codes[16]; for (uint i = 0; i < 16; ++i) { - opcodes[i] = 0xFFFF; + codes[i] = 0xFFFF; } uint samples = 0; @@ -123,10 +114,29 @@ int main(void) { if (cpuEq(result, after)) { count++; } else { - opcodes[code] &= ~(1 << i); + codes[code] &= ~(1 << i); } } if (count >= 3) samples++; } printf("%u\n", samples); + + Set known = 0; + while (known != 0xFFFF) { + for (uint i = 0; i < 16; ++i) { + if (__builtin_popcount(codes[i]) == 1) { + known |= codes[i]; + } else { + codes[i] &= ~known; + } + } + } + + struct CPU cpu = { .r = { 0, 0, 0, 0 } }; + while (!feof(stdin)) { + uint code, a, b, c; + scanf("%u %u %u %u\n", &code, &a, &b, &c); + cpu = Ops[__builtin_ctz(codes[code])].fn(cpu, a, b, c); + } + printf("%u\n", cpu.r[0]); } |