-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (63 loc) · 2.43 KB
/
main.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
from itertools import product
class Day14:
def __init__(self):
self.puzzle_input = self.load_input()
def part_one(self) -> int:
current_mask, memory = [], {}
for line in self.puzzle_input:
split = line.split(" ")
if "mask" in split:
current_mask = split[-1]
else:
key = int("".join(filter(str.isdigit, split[0])))
value = self.decimal_to_binary(split[-1])
value_masked = "".join(
[val if current_mask[i] == "X" else current_mask[i] for i, val in enumerate(value)]
)
memory[key] = self.binary_to_decimal(value_masked)
return sum(memory.values())
def part_two(self) -> int:
current_mask, memory = [], {}
for line in self.puzzle_input:
split = line.split(" ")
if "mask" in split:
current_mask = split[-1]
else:
key = self.decimal_to_binary(int("".join(filter(str.isdigit, split[0]))))
value = split[-1]
key_masked = "".join(
[
current_mask[i] if current_mask[i] == "X" or current_mask[i] == "1" else val
for i, val in enumerate(key)
]
)
masks = self.get_mask_combinations(key_masked)
for mask in masks:
memory[self.binary_to_decimal(mask)] = int(value)
return sum(memory.values())
@staticmethod
def decimal_to_binary(decimal: int) -> str:
return f"{int(decimal):036b}"
@staticmethod
def binary_to_decimal(binary: str) -> int:
return int(str(binary), 2)
@staticmethod
def get_mask_combinations(mask: str) -> list:
combinations = list(product([0, 1], repeat=mask.count("X")))
masks = []
for combination in combinations:
tmp_mask = mask
for num in combination:
tmp_mask = tmp_mask.replace("X", str(num), 1)
masks.append(tmp_mask)
return masks
@staticmethod
def load_input() -> list:
list_input = []
with open("input.txt", encoding="utf-8") as file:
for line in file:
list_input.append(line.rstrip())
return list_input
day = Day14()
print(f"Result part 1: {day.part_one()}")
print(f"Result part 2: {day.part_two()}")