Day 14 (glamorized)
This commit is contained in:
@@ -3,19 +3,19 @@ import re
|
|||||||
lines = (x.strip() for x in open("input.txt"))
|
lines = (x.strip() for x in open("input.txt"))
|
||||||
mem = {}
|
mem = {}
|
||||||
regex_mask = re.compile(r"mask = (.+)")
|
regex_mask = re.compile(r"mask = (.+)")
|
||||||
regex_mem = re.compile(r"mem\[(.*)] = (.+)")
|
regex_mem = re.compile(r"mem\[(.+)] = (.+)")
|
||||||
mask_and = 0
|
mask_and = 0
|
||||||
mask_or = -1
|
mask_or = -1
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
match = regex_mask.match(line)
|
match = regex_mask.match(line)
|
||||||
if match is not None:
|
if match is not None:
|
||||||
mask = match.group(1)[::-1]
|
mask = match.group(1)
|
||||||
mask_and = sum(2**i for i, v in enumerate(mask) if v != '0')
|
mask_and = int(mask.replace('X', '1'), 2)
|
||||||
mask_or = sum(2**i for i, v in enumerate(mask) if v == '1')
|
mask_or = int(mask.replace('X', '0'), 2)
|
||||||
else:
|
else:
|
||||||
match = regex_mem.match(line)
|
match = regex_mem.match(line)
|
||||||
addr, dec = list(map(int, match.groups()))
|
addr, dec = list(map(int, match.groups()))
|
||||||
mem[addr] = dec & mask_and | mask_or
|
mem[addr] = dec & mask_and | mask_or
|
||||||
|
|
||||||
print(sum(map(lambda x: mem[x], mem)))
|
print(sum(mem.values()))
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import re
|
|||||||
lines = (x.strip() for x in open("input.txt"))
|
lines = (x.strip() for x in open("input.txt"))
|
||||||
mem = {}
|
mem = {}
|
||||||
regex_mask = re.compile(r"mask = (.+)")
|
regex_mask = re.compile(r"mask = (.+)")
|
||||||
regex_mem = re.compile(r"mem\[(.*)] = (.+)")
|
regex_mem = re.compile(r"mem\[(.+)] = (.+)")
|
||||||
mask = ""
|
mask = ""
|
||||||
|
|
||||||
|
|
||||||
@@ -31,4 +31,4 @@ for line in lines:
|
|||||||
for x in combination(res):
|
for x in combination(res):
|
||||||
mem[x] = dec
|
mem[x] = dec
|
||||||
|
|
||||||
print(sum(map(lambda x: mem[x], mem)))
|
print(sum(mem.values()))
|
||||||
|
|||||||
Reference in New Issue
Block a user