summary refs log tree commit diff homepage
path: root/2020
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-14 00:26:14 -0500
committerJune McEnroe <june@causal.agency>2020-12-14 00:29:17 -0500
commitb68d3719040e5d3b7243f36cca1454b2482f351d (patch)
treed1ab92844544541c576b99735dc9ae48622a30a9 /2020
parentSpoiled solve day 13 part 2 (diff)
downloadaoc-b68d3719040e5d3b7243f36cca1454b2482f351d.tar.gz
aoc-b68d3719040e5d3b7243f36cca1454b2482f351d.zip
Solve day 14 part 1
Diffstat (limited to '2020')
-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);
+}