summary refs log tree commit diff homepage
path: root/2020/day14.c
diff options
context:
space:
mode:
Diffstat (limited to '2020/day14.c')
-rw-r--r--2020/day14.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/2020/day14.c b/2020/day14.c
new file mode 100644
index 0000000..f04e864
--- /dev/null
+++ b/2020/day14.c
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <stdlib.h>
+typedef unsigned long ul;
+static struct {
+	ul addr;
+	ul val;
+} writes[1024];
+static int len;
+static void write(ul addr, ul val) {
+	for (int i = 0; i < len; ++i) {
+		if (writes[i].addr == addr) {
+			writes[i].val = val;
+			return;
+		}
+	}
+	writes[len].addr = addr;
+	writes[len++].val = val;
+}
+int main(void) {
+	ul mask = 0;
+	ul bits = 0;
+	for (;;) {
+		char str[37];
+		int n = scanf("mask = %s\n", str);
+		if (n < 0) break;
+		if (n) {
+			mask = 0;
+			bits = 0;
+			for (ul i = 35; i < 36; --i) {
+				if (str[35-i] == 'X') {
+					mask |= 1UL << i;
+				} else if (str[35-i] == '1') {
+					bits |= 1UL << i;
+				}
+			}
+		} else {
+			ul addr, val;
+			n = scanf("em[%lu] = %lu\n", &addr, &val);
+			if (n < 2) break;
+			val &= mask;
+			val |= bits;
+			write(addr, val);
+		}
+	}
+	ul sum = 0;
+	for (int i = 0; i < len; ++i) {
+		sum += writes[i].val;
+	}
+	printf("%lu\n", sum);
+}