summary refs log tree commit diff homepage
path: root/2018
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-12-17 16:14:16 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commitaf99d049942863c5d5d4b6769cebee4b9e16ceb8 (patch)
treea0ee5238e91256a7208b729727b9352ef72b1d92 /2018
parentSolve day 16 part 1 (diff)
downloadaoc-af99d049942863c5d5d4b6769cebee4b9e16ceb8.tar.gz
aoc-af99d049942863c5d5d4b6769cebee4b9e16ceb8.zip
Solve day 16 part 2
Diffstat (limited to '2018')
-rw-r--r--2018/day16.c34
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]);
 }