adventofcode-2020/14/part1

20 lines
596 B
Plaintext
Raw Normal View History

2020-12-14 22:42:53 +01:00
#!/usr/bin/python3
import re
mem = []
with open("input") as fh:
for ln in fh:
if m := re.match(r"mask = ([01X]{36})", ln):
mask = m.group(1)
mask_and = int(mask.replace("1", "0").replace("X", "1"), base=2)
mask_or = int(mask.replace("X", "0"), base=2)
elif m:= re.match(r"mem\[(\d+)\] = (\d+)", ln):
i = int(m.group(1))
v = int(m.group(2))
while i >= len(mem):
mem.append(0)
mem[i] = (v & mask_and) | mask_or
else:
raise RuntimeError(ln)
print(sum(mem))