This repository was archived by the owner on Dec 25, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpart1.py
76 lines (56 loc) · 1.54 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
68
69
70
71
72
73
74
75
76
from __future__ import annotations
import argparse
import collections
import itertools
import os.path
import pytest
import support
INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt')
def compute(s: str) -> int:
beacons: dict[str, list[tuple[int, int]]]
beacons = collections.defaultdict(list)
for y, line in enumerate(s.splitlines()):
for x, c in enumerate(line):
if c != '.':
beacons[c].append((x, y))
bx = range(0, len(s.splitlines()[0]))
by = range(0, len(s.splitlines()))
antinodes = set()
for nodes in beacons.values():
for (x1, y1), (x2, y2) in itertools.combinations(nodes, 2):
dx, dy = x2 - x1, y2 - y1
antinodes.add((x2 + dx, y2 + dy))
antinodes.add((x1 - dx, y1 - dy))
return len({(x, y) for x, y in antinodes if x in bx if y in by})
INPUT_S = '''\
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
'''
EXPECTED = 14
@pytest.mark.parametrize(
('input_s', 'expected'),
(
(INPUT_S, EXPECTED),
),
)
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, support.timing():
print(compute(f.read()))
return 0
if __name__ == '__main__':
raise SystemExit(main())