-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
executable file
·113 lines (88 loc) · 3.3 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from itertools import product
from typing import Dict, List, Tuple
MEMORY_SIZE = 36
class Mask:
def __init__(self, mask: str):
self._raw = mask[len('mask = '):]
self._bit_masks = {}
self._floating_masks = []
for idx, c in enumerate(self._raw):
if c != 'X':
self._bit_masks[MEMORY_SIZE - (idx + 1)] = int(c)
else:
self._floating_masks.append(MEMORY_SIZE - (idx + 1))
def __repr__(self) -> str:
return self._raw
@property
def bits(self) -> Dict[int, int]:
return self._bit_masks
@property
def floaters(self) -> List[int]:
return self._floating_masks
@staticmethod
def mask_value(value: int, loc: int, bit: int) -> int:
"""
For example:
mask_value(0, 1, 1) flips the rightmost bit to 1
--> old = 0 --> 0000
--> value = 1 --> 0001
mask_value(1, 1, 0) flips the rightmost bit to 0
--> old = 1 --> 0001
--> value = 0 --> 0000
"""
if bit == 1:
return value | (1 << loc)
if bit == 0:
return value &~ (1 << loc)
def get_sum_of_values_in_memory_1(instructions: Dict[str, List[Tuple[int, int]]]) -> int:
memory = {}
for mask, instruction in instructions.items():
for index, value in instruction:
for loc, bit in mask.bits.items():
value = Mask.mask_value(value, loc, bit)
memory[index] = value
return sum(memory.values())
def get_sum_of_values_in_memory_2(instructions: Dict[str, List[Tuple[int, int]]]) -> int:
memory = {}
for mask, instruction in instructions.items():
for address, value in instruction:
overflown_addresses = []
for loc, bit in mask.bits.items():
if bit == 1:
address = Mask.mask_value(address, loc, bit)
for combo in list(product(range(2), repeat=len(mask.floaters))):
new_address = address
for bit_placement in zip(mask.floaters, combo):
loc, bit = bit_placement
new_address = Mask.mask_value(new_address, loc, bit)
overflown_addresses.append(new_address)
for overflown_address in overflown_addresses:
memory[overflown_address] = value
return sum(memory.values())
if __name__ == '__main__':
instructions = {}
with open('test.txt', 'r') as f:
current_mask = None
for line in f.readlines():
line = line.strip()
if line.startswith('mask'):
mask = Mask(line)
instructions[mask] = []
current_mask = mask
else:
index = int(line[len('mem['): line.index(']')])
value = int(line[line.index('=')+1:])
instructions[current_mask].append((index, value))
"""
instruction --> {
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X: [
('8', 11), # memory location (8) and value to be written (11)
('7', 101),
('8', 0)
]
}
"""
# Part 1
print(f'Part 1: {get_sum_of_values_in_memory_1(instructions)}')
# Part 2
print(f'Part 2: {get_sum_of_values_in_memory_2(instructions)}')