2020 Day 14 meme edition

This commit is contained in:
Luke Hubmayer-Werner 2020-12-14 19:30:01 +10:30
parent e2b9ee38c2
commit e6ab51fdd5
1 changed files with 41 additions and 0 deletions

41
2020/day14_meme.py Normal file
View File

@ -0,0 +1,41 @@
with open('input14', 'r') as file:
lines = [l.strip() for l in file.readlines()]
def masks(m):
m_reset = int(m.replace('X', '1'), 2)
m_set = int(m.replace('X', '0'), 2)
return m_reset, m_set
mem = {}
m_reset = 0xFFFFFFFFFFFFFFFF
m_set = 0
for line in lines:
tokens = line.split()
if tokens[0] == 'mask':
m_reset, m_set = masks(tokens[-1])
elif tokens[0][:3] == 'mem':
mem[int(tokens[0][4:-1])] = int(tokens[-1]) & m_reset | m_set
print(f'Part 1: {sum(mem.values())}')
def mem_masks(m):
return m_set, fork_str
mem = {}
fork_str = ''
xs = 0
m_set = 0
for line in lines:
tokens = line.split()
if tokens[0] == 'mask':
fork_str = tokens[-1] #.replace('X', '{}')
xs = fork_str.count('X')
m_set = int(tokens[-1].replace('X', '0'), 2)
elif tokens[0][:3] == 'mem':
val = int(tokens[-1])
addr = f'{int(tokens[0][4:-1]) | m_set:036b}'
addr = ''.join(['{}' if f=='X' else c for c,f in zip(addr, fork_str)])
addrs = [addr.format(*f'{i:0{xs}b}') for i in range(2**xs)]
for a in addrs:
mem[a] = val
print(f'Part 2: {sum(mem.values())}')