22 lines
558 B
Python
22 lines
558 B
Python
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)
|
|
mask_and = int(mask.replace('X', '1'), 2)
|
|
mask_or = int(mask.replace('X', '0'), 2)
|
|
else:
|
|
match = regex_mem.match(line)
|
|
addr, dec = list(map(int, match.groups()))
|
|
mem[addr] = dec & mask_and | mask_or
|
|
|
|
print(sum(mem.values()))
|