-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
61 lines (56 loc) · 1.63 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
58
59
60
61
import re
import itertools
with open("input/day14", "r") as input:
data = input.read().split("\n")
data.pop()
ones = 0 # all bits 1 in mask set to 1
zeroes = 0 # all bits *not* 0 in mask set to 1 (including 'X')
mem = {}
for entry in data:
match = re.match(r"mask = (.+)", entry)
if match:
ones = 0
zeroes = 0
m = match[1]
for idx in range(len(m) - 1, -1, -1):
c = m[idx]
incr = 2**(len(m) - idx - 1)
if c == 'X':
zeroes += incr
elif c == '1':
ones += incr
zeroes += incr
else:
match = re.match(r"mem\[(.+)\] = (.+)", entry)
addr = int(match[1])
val = int(match[2])
mem[addr] = (val | ones) & zeroes
print("Part 1: " + str(sum(mem.values())))
all_x = 0 # all bits *not* 'X' in mask set to 1
permutations = [0]
mem = {}
for entry in data:
match = re.match(r"mask = (.+)", entry)
if match:
ones = 0
all_x = 0
permutations = [0]
m = match[1]
for idx in range(len(m) - 1, -1, -1):
c = m[idx]
incr = 2**(len(m) - idx - 1)
if c == 'X':
permutations += [p + incr for p in permutations]
elif c == '1':
ones += incr
all_x += incr
elif c == '0':
all_x += incr
else:
match = re.match(r"mem\[(.+)\] = (.+)", entry)
addr = int(match[1])
val = int(match[2])
addr = (addr | ones) & all_x
for p in permutations:
mem[addr | p] = val
print("Part 2: " + str(sum(mem.values())))