This commit is contained in:
Sebastian Seedorf
2020-12-14 09:54:56 +01:00
parent dd74756918
commit 8f140f8e00
3 changed files with 597 additions and 0 deletions

21
day14/part1.py Normal file
View File

@@ -0,0 +1,21 @@
import re
lines = (x.strip() for x in open("input.txt"))
mem = {}
regex_mask = re.compile(r"mask = (.+)")
regex_mem = re.compile(r"mem\[(.*)] = (.+)")
mask_and = 0
mask_or = -1
for line in lines:
match = regex_mask.match(line)
if match is not None:
mask = match.group(1)[::-1]
mask_and = sum(2**i for i, v in enumerate(mask) if v != '0')
mask_or = sum(2**i for i, v in enumerate(mask) if v == '1')
else:
match = regex_mem.match(line)
addr, dec = list(map(int, match.groups()))
mem[addr] = dec & mask_and | mask_or
print(sum(map(lambda x: mem[x], mem)))