-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_14.py
More file actions
38 lines (33 loc) · 1.45 KB
/
day_14.py
File metadata and controls
38 lines (33 loc) · 1.45 KB
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
#%% Part 1
import re
mask_regex = re.compile(r"^mask\s+=\s+([X10]+)")
mem_regex = re.compile(r"^mem\[(\d+)\]\s=\s(\d+)")
result = {}
with open("day_14_input.txt") as input_data:
for line in input_data:
if mask_match := mask_regex.match(line):
and_pattern = int(mask_match.group(1).replace("1", "0").replace("X", "1"), 2)
or_pattern = int(mask_match.group(1).replace("X", "0"), 2)
else:
position, value = map(int, mem_regex.match(line).groups())
result[position] = value & and_pattern | or_pattern
print(f"Sum of remaining values: {sum(result.values())}")
#%% Part 2
result = {}
with open("day_14_input.txt") as input_data:
for line in input_data:
if mask_match := mask_regex.match(line):
and_pattern = int(mask_match.group(1).replace("0", "1").replace("X", "0"), 2)
or_template = mask_match.group(1)
or_patterns = []
x_count = line.count("X")
for x in range(2**x_count):
or_pattern = or_template
for c in bin(x)[2:].zfill(x_count):
or_pattern = or_pattern.replace("X", c, 1)
or_patterns.append(int(or_pattern, 2))
else:
position, value = map(int, mem_regex.match(line).groups())
for or_pattern in or_patterns:
result[position & and_pattern | or_pattern] = value
print(f"Sum of remaining values: {sum(result.values())}")