From e6ab51fdd560d4320bcfd013a4cd016409303f78 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Mon, 14 Dec 2020 19:30:01 +1030 Subject: [PATCH] 2020 Day 14 meme edition --- 2020/day14_meme.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2020/day14_meme.py diff --git a/2020/day14_meme.py b/2020/day14_meme.py new file mode 100644 index 0000000..3730520 --- /dev/null +++ b/2020/day14_meme.py @@ -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())}')