summary refs log tree commit diff homepage
path: root/2018/day16.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-12-17 16:14:16 -0500
committerJune McEnroe <june@causal.agency>2018-12-17 16:14:16 -0500
commit4b60c93536d20a68ca2be5d0fa57b9afc19fa064 (patch)
tree72c3e6ddd50f3917851502d55e0b4b986db5c722 /2018/day16.c
parentSolve day 16 part 1 (diff)
downloadaoc-4b60c93536d20a68ca2be5d0fa57b9afc19fa064.tar.gz
aoc-4b60c93536d20a68ca2be5d0fa57b9afc19fa064.zip
Solve day 16 part 2
Diffstat (limited to '')
-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]);
 }
c881d11da66055e6955e1c861f0a838d5586e81&follow=1'>Don't match nested parentheses in Tag for CJune McEnroe 2019-02-18Match whitespace between * [] {}June McEnroe 2019-02-18Fix function-like #define regexJune McEnroe 2019-02-18Match Tag in RustJune McEnroe 2019-02-18Match sh functions as TagJune McEnroe 2019-02-18Match Sh and Ss as Tag in mdocJune McEnroe 2019-02-18Match statics and typedefs as TagJune McEnroe 2019-02-18Clean up htmlHeaderJune McEnroe 2019-02-18Remove hi line numberingJune McEnroe 2019-02-18Add Tag class to hiJune McEnroe 2019-02-17Generate HTML with hi -n -f html -o anchorJune McEnroe 2019-02-17Add hi -f html -o anchor for line number linksJune McEnroe 2019-02-17Simplify temp trap in upJune McEnroe 2019-02-17Add line numbers to hiJune McEnroe 2019-02-17Always split spans after newlinesJune McEnroe 2019-02-15Color format specifiers light cyan in vimJune McEnroe 2019-02-15Highlight Interp as yellowJune McEnroe 2019-02-15Highlight strings in sh command substitutionsJune McEnroe 2019-02-15Add nmap gpJune McEnroe 2019-02-14Avoid newline when copying URL to pasteboardJune McEnroe 2019-02-13Add forgotten "sixth" book of H2G2June McEnroe