-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.py
42 lines (34 loc) · 1.11 KB
/
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
from lib import *
input = read_input(2020, 14)
lines = input.splitlines()
mem = {}
mask = "X" * 36
s = 0
for line in lines:
if match := re.match(r"^mask = ([01X]+)$", line):
mask = match[1]
elif match := re.match(r"^mem\[(\d+)\] = (\d+)$", line):
a, v = map(int, match.groups())
s -= mem.get(a, 0)
mem[a] = int("".join(x if m == "X" else m for m, x in zip(mask, bin(v)[2:].zfill(36))), 2)
s += mem[a]
print(s)
mem = {}
mask = "X" * 36
s = 0
for line in lines:
if match := re.match(r"^mask = ([01X]+)$", line):
mask = match[1]
elif match := re.match(r"^mem\[(\d+)\] = (\d+)$", line):
a, v = map(int, match.groups())
a = "".join(x if m == "0" else m for m, x in zip(mask, bin(a)[2:].zfill(36)))
floating = [i for i in range(36) if a[i] == "X"]
for i in range(1 << len(floating)):
ad = [*a]
for j in range(len(floating)):
ad[floating[j]] = min((1 << j) & i, 1)
ad = int("".join(map(str, ad)), 2)
s -= mem.get(ad, 0)
mem[ad] = v
s += mem[ad]
print(s)