-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp08.py
52 lines (41 loc) · 1.1 KB
/
p08.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
from itertools import combinations
from aocd import data
city_map = {}
antennas = set()
antinodes = set()
antinodes2 = set()
for y, row in enumerate(data.splitlines()):
for x, col in enumerate(row):
city_map[(x, y)] = col
if col.isalnum():
antennas.add((x, y))
for a, b in combinations(antennas, 2):
if city_map[a] == city_map[b]:
x1, y1 = a
x2, y2 = b
mx = x2 - x1
my = y2 - y1
node1 = (x1 - mx, y1 - my)
node2 = (x2 + mx, y2 + my)
if node1 in city_map:
antinodes.add(node1)
if node2 in city_map:
antinodes.add(node2)
nx, ny = a
while True:
nx -= mx
ny -= my
if (nx, ny) in city_map:
antinodes2.add((nx, ny))
else:
break
nx, ny = b
while True:
nx += mx
ny += my
if (nx, ny) in city_map:
antinodes2.add((nx, ny))
else:
break
print("Part 1:", len(antinodes))
print("Part 2:", len(antinodes2 | antennas))