-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.py
57 lines (47 loc) · 1.56 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
import numpy as np
inp_f = open("input/test.txt", "r")
inp = inp_f.read()
inp_list = [line for line in inp.split("\n") if line != ""]
def part1():
mem = {}
mask = [None, None]
for line in inp_list:
assign = line.split(" = ")
if assign[0] == "mask":
str_mask = assign[1]
mask[0] = int(str_mask.replace("X", "1"), 2)
mask[1] = int(str_mask.replace("X", "0"), 2)
else:
key = assign[0].split("[")[1][:-1]
old_val = int(assign[1])
mem[key] = old_val & mask[0] | mask[1]
return sum(mem.values())
def part2():
mem = {}
mask = [None, None, None]
for line in inp_list:
assign = line.split(" = ")
if assign[0] == "mask":
str_mask = assign[1]
# mask to turn X's -> 0's
step1 = str_mask.replace("0", "1")
mask[0] = int(step1.replace("X", "0"), 2)
# 1's mask
mask[1] = int(str_mask.replace("X", "0"), 2)
# X positions
idxs = list(np.where(np.array(list(str_mask)) == "X")[0])
mask[2] = [35 - idx for idx in idxs]
else:
key = int(assign[0].split("[")[1][:-1])
key = key & mask[0]
key = key | mask[1]
to_add = [0]
for idx in mask[2]:
to_add += [2 ** idx + e for e in to_add]
val = int(assign[1])
for add in to_add:
mem[key+add] = val
return sum(mem.values())
if __name__ == "__main__":
print(part1())
print(part2())