-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.py
35 lines (30 loc) · 1.02 KB
/
day08.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
from collections import defaultdict
input = open("inputs/day08.txt").read().split("\n")
antennas = defaultdict(list)
for y, line in enumerate(input):
for x, c in enumerate(line):
if c != ".":
antennas[c].append((y, x))
def inbounds(y, x):
return 0 <= y < len(input) and 0 <= x < len(input[0])
def add_inbounds(s, val):
if inbounds(*val):
s.add(val)
antinodes = set()
antinodes2 = set()
for antenna, coords in antennas.items():
for i, (ay, ax) in enumerate(coords):
for (by, bx) in coords[i+1:]:
dy, dx = ay - by, ax - bx
add_inbounds(antinodes, (ay + dy, ax + dx))
add_inbounds(antinodes, (by - dy, bx - dx))
y, x = ay, ax
while inbounds(y, x):
antinodes2.add((y, x))
y, x = y + dy, x + dx
y, x = ay, ax
while inbounds(y, x):
antinodes2.add((y, x))
y, x = y - dy, x - dx
print(f"part 1 = {len(antinodes)}")
print(f"part 2 = {len(antinodes2)}")