This repository was archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpart1.py
67 lines (49 loc) · 1.39 KB
/
part1.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
import argparse
import collections
import os.path
import re
import pytest
from support import timing
INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt')
MEM_RE = re.compile(r'^mem\[(\d+)\] = (\d+)$')
def compute(s: str) -> int:
memory = collections.defaultdict(int)
mask_or = 0
mask_and = -1
lines = s.splitlines()
for line in lines:
if line.startswith('mask'):
_, _, mask_s = line.partition(' = ')
mask_or = int(mask_s.replace('X', '0'), 2)
mask_and = int(mask_s.replace('X', '1'), 2)
else:
match = MEM_RE.match(line)
assert match
target = int(match[1])
number = int(match[2])
masked = (number | mask_or) & mask_and
memory[target] = masked
return sum(memory.values())
INPUT_S = '''\
mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
mem[8] = 11
mem[7] = 101
mem[8] = 0
'''
@pytest.mark.parametrize(
('input_s', 'expected'),
(
(INPUT_S, 165),
),
)
def test(input_s: str, expected: int) -> None:
assert compute(input_s) == expected
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('data_file', nargs='?', default=INPUT_TXT)
args = parser.parse_args()
with open(args.data_file) as f, timing():
print(compute(f.read()))
return 0
if __name__ == '__main__':
exit(main())