-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc_2020_14.py
58 lines (49 loc) · 1.45 KB
/
aoc_2020_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
50
51
52
53
54
55
56
57
58
import re
input_file = open('14.input.txt', 'r')
input = input_file.readlines()
memory = {}
mask_and = 0
mask_or = 0
mask_pattern = ""
def map_to_mask(mask):
global mask_or
global mask_and
global mask_pattern
mask_or = int(re.sub(r"X", "0", mask), 2)
mask_and = int(re.sub(r"X", "1", mask), 2)
mask_pattern = mask.strip()
def extend_with(b, arr):
return [b + x for x in arr]
def gen_replaced(add, mask):
if len(mask) == 1:
if mask == "X":
return ['0', '1']
elif mask == "0":
return [add[0]]
else:
return ['1']
rest = gen_replaced(add[1:], mask[1:])
if mask[0] == "X":
return extend_with('0', rest) + extend_with('1', rest)
elif mask[0] == "1":
return extend_with('1', rest)
else:
return extend_with(add[0], rest)
def gen_addresses(address):
a = "{0:036b}".format(address)
print("mask: {} ({}), a: {}, ({})".format(mask_pattern, len(mask_pattern), a, len(a)))
r = [int(x, 2) for x in gen_replaced(a, mask_pattern)]
print(gen_replaced(a, mask_pattern))
return r
COMMAND_PATTERN = re.compile("mem\[(\d+)\] = (\d+)")
for cmd in input:
if cmd[:4] == "mask":
map_to_mask(cmd[6:])
continue
match = COMMAND_PATTERN.match(cmd)
address = int(match.group(1)) | mask_or
for a in gen_addresses(address):
memory[a] = int(match.group(2))
print(sum(memory.values()))
print(mask)
print(commands)