-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.py
60 lines (53 loc) · 1.47 KB
/
8.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
# pylint: skip-file
# mypy: ignore-errors
# flake8: noqa
from collections import defaultdict
from math import gcd
input_value = open("8.txt", "r").read()
lines = input_value.split("\n")
grid = defaultdict(lambda: None)
antennas = set()
for r in range(len(lines)):
for c in range(len(lines[r])):
grid[r, c] = lines[r][c]
if grid[r, c] != ".":
antennas.add((r, c))
antinodes = set()
for r1, c1 in antennas:
for r2, c2 in antennas:
if r1 == r2 and c1 == c2:
continue
if grid[r1, c1] != grid[r2, c2]:
continue
dr = r2 - r1
dc = c2 - c1
anti_1 = (r1 - dr, c1 - dc)
anti_2 = (r2 + dr, c2 + dc)
if grid[anti_1] is not None:
antinodes.add(anti_1)
if grid[anti_2] is not None:
antinodes.add(anti_2)
# Part 1:
print(len(antinodes))
antinodes = set()
for r1, c1 in antennas:
for r2, c2 in antennas:
if r1 == r2 and c1 == c2:
continue
if grid[r1, c1] != grid[r2, c2]:
continue
dr = r2 - r1
dc = c2 - c1
divisor = gcd(dr, dc)
dr /= divisor
dc /= divisor
k = 0
while grid[r1 + k * dr, c1 + k * dc] is not None:
antinodes.add((r1 + k * dr, c1 + k * dc))
k += 1
k = 0
while grid[r1 + k * dr, c1 + k * dc] is not None:
antinodes.add((r1 + k * dr, c1 + k * dc))
k -= 1
# Part 2:
print(len(antinodes))