-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.part2.py
53 lines (40 loc) · 1.43 KB
/
day14.part2.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
import re
MEMORY_REGEX = re.compile("^mem\\[([0-9]+)\\] = ([0-9]+)$")
input_filename = __file__.split(".")[0] + ".input"
with open(input_filename) as f:
raw = f.read().strip().split("\n")
def _get_addresses(address, mask):
address = address.zfill(len(mask))
addresses = ['']
for i, c in enumerate(mask):
if c == '0': # unchanged
for idx, _ in enumerate(addresses):
addresses[idx] += address[i]
elif c == '1': # force 1
for idx, _ in enumerate(addresses):
addresses[idx] += '1'
elif c == 'X': # quantum bit
addresses *= 2 # double number of addresses
half = int(len(addresses)/2)
for idx, _ in enumerate(addresses[:half]):
addresses[idx] += '0'
for idx, _ in enumerate(addresses[half:], half):
addresses[idx] += '1'
return addresses
import pdb;pdb.set_trace()
memory = {}
current_bitmask = None
for line in raw:
if line.startswith("mask"):
_, current_bitmask = line.split(" = ")
else:
address, value = re.match(MEMORY_REGEX, line).groups()
value = int(value)
address = bin(int(address)).split('b')[1]
addresses = _get_addresses(address, current_bitmask)
for adr in addresses:
memory[adr] = value
answer = sum(memory.values())
print(answer)
if answer <= 3812194945915:
print("It's probably wrong")