-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday14.py
executable file
·60 lines (48 loc) · 1.36 KB
/
day14.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import sys
import re
from itertools import product
def all_addrs(addr, mask):
args = []
for addr_bit, mask_bit in zip(addr, mask):
if mask_bit == '0':
args.append(addr_bit)
elif mask_bit == '1':
args.append('1')
else:
args.append('01')
for a in product(*args):
yield int(''.join(a), 2)
# Alternative manual implementation.
#
# def all_addrs(addr, mask=None):
# if mask is not None:
# addr = ''.join(a if m == '0' else m for a, m in zip(addr, mask))
#
# if 'X' in addr:
# yield from all_addrs(addr.replace('X', '0', 1))
# yield from all_addrs(addr.replace('X', '1', 1))
# else:
# yield int(addr, 2)
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
lines = fin.readlines()
rexp = re.compile(r'mem\[(\d+)\] = (\d+)')
table = str.maketrans('1X', '01')
mem1 = {}
mem2 = {}
for line in lines:
if line.startswith('mask'):
mask = line[7:].rstrip()
mask_clear = int(mask.translate(table), 2)
mask_set = int(mask.replace('X', '0'), 2)
else:
addr, value = map(int, rexp.findall(line)[0])
mem1[addr] = (value & mask_clear) | mask_set
addr = '{:036b}'.format(addr)
for a in all_addrs(addr, mask):
mem2[a] = value
total1 = sum(mem1.values())
total2 = sum(mem2.values())
print('Part 1:', total1)
print('Part 2:', total2)