-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
93 lines (83 loc) · 2.61 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import re
import itertools
import functools
from aoc import read_file, timer
memory = {}
TEST = """mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
mem[8] = 11
mem[7] = 101
mem[8] = 0""".split('\n')
TEST2 = """mask = 000000000000000000000000000000X1001X
mem[42] = 100
mask = 00000000000000000000000000000000X0XX
mem[26] = 1""".split('\n')
MEM = re.compile(r'mem\[(\d+)\] = (\d+)')
def part1(data):
mask = None
one_mask = 0
zero_mask = 0
for line in data:
if line.startswith('mask'):
mask = line[line.index('=') + 2:]
one_mask = 0
zero_mask = 0
for c in mask:
one_mask <<= 1
zero_mask <<= 1
if c == '1':
one_mask |= 1
elif c == '0':
zero_mask |= 1
else:
match = MEM.match(line)
address, value = int(match.group(1)), int(match.group(2))
masked_value = (value | one_mask) & (0b111111111111111111111111111111111111 ^ zero_mask)
memory[address] = masked_value
def part2(data):
mask = None
one_mask = 0
zero_mask = 0
float_mask = 0
float_offsets = []
for line in data:
if line.startswith('mask'):
mask = line[line.index('=') + 2:]
one_mask = 0
zero_mask = 0
float_mask = 0
float_offset = 1
float_offsets = []
for c in mask:
one_mask <<= 1
zero_mask <<= 1
float_mask <<= 1
if c == '1':
one_mask |= 1
elif c == '0':
zero_mask |= 1
elif c == 'X':
float_mask |= 1
for c in mask[::-1]:
if c == 'X':
float_offsets.append(float_offset)
print(f"{float_offset=:>036b} {float_offset}")
float_offset <<= 1
else:
combinations = \
[
functools.reduce(lambda a, b : a | b, x)
for x in list(itertools.product(*((c, 0) for c in float_offsets)))
]
match = MEM.match(line)
address, value = int(match.group(1)), int(match.group(2))
address |= one_mask
address &= (0b111111111111111111111111111111111111 ^ float_mask)
for c in combinations:
memory[address | c] = value
if __name__ == "__main__":
data = read_file(14)
# part1(TEST)
# part1(data)
# part2(TEST2)
part2(data)
print(sum(memory.values()))