-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14_1.py
30 lines (27 loc) · 1.05 KB
/
day14_1.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
class Day14_1:
input = """"""
def __init__(self):
insts = self.input.split('\n')
mem = [0 for i in range(66000)]
mask = ''
address = None
for inst in insts:
typ, val = inst.split(' = ')
if typ == 'mask':
mask = val
elif 'mem' in typ:
as_binary_string = lambda x, n: format(x, 'b').zfill(n)
value = int(val)
address = typ[4::]
address = int(address[:-1])
address_binary = list(as_binary_string(value, 36))
for index, bit in enumerate(list(mask)[::-1]):
if bit == '1':
address_binary[len(address_binary) - index - 1] = '1'
value = int(''.join(address_binary), 2)
elif bit == '0':
address_binary[len(address_binary) - index - 1] = '0'
value = int(''.join(address_binary), 2)
mem[address] = value
print(sum(mem))
Day14_1()