-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass-day-14.py
57 lines (38 loc) · 1.06 KB
/
ass-day-14.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
def to_bitstr(i, mask, l=36):
bitstr = list(str(bin(i))[2:].zfill(l))
for m in mask:
idx, val = m
bitstr[idx] = str(val)
return ''.join(bitstr)
def get_masklist(mask):
lst = []
for idx, val in enumerate(mask):
if val !='X':
lst.append((idx, val))
return lst
def construct_command(command):
if command[:3] == 'mem':
mem, val = command.split(' = ')
mem_addr = mem[4:][:-1]
return 'mem', int(mem_addr), int(val)
elif command[:4] == 'mask':
mem, val = command.split(' = ')
return 'mask', 0, val
else:
print(f"Unknown command: {command}")
# Read input
f = open("ass-day-14-input.txt", "r")
commands = f.read().split("\n")
mask = []
memory = {}
for c in commands:
command, mem_addr, val = construct_command(c)
if command == 'mask':
mask = get_masklist(val)
elif command == 'mem':
memory[mem_addr] = to_bitstr(val, mask)
# Sum memory
sum = 0
for idx in memory:
sum += int('0b'+memory[idx], 2)
print(f"Sum of memory: {sum}")