Skip to content

Commit 050688f

Browse files
committed
Add solution to 2024-12-08
1 parent b8c3992 commit 050688f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

2024/day08/solutions.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from collections import defaultdict
2+
from itertools import count, permutations
3+
4+
with open("input") as f:
5+
ls = f.read().strip().split("\n")
6+
7+
board = {i + 1j * j: x for i, l in enumerate(ls) for j, x in enumerate(l)}
8+
antennas = defaultdict(list)
9+
for z, x in board.items():
10+
if x not in (".", "#"):
11+
antennas[x].append(z)
12+
13+
# Part 1
14+
antinodes = {
15+
2 * z2 - z1 for zs in antennas.values() for z1, z2 in permutations(zs, 2)
16+
} & board.keys()
17+
print(len(antinodes))
18+
19+
# Part 2
20+
antinodes = set()
21+
for zs in antennas.values():
22+
for z1, z2 in permutations(zs, 2):
23+
for i in count():
24+
z = z2 + i * (z2 - z1)
25+
if z not in board:
26+
break
27+
antinodes.add(z)
28+
print(len(antinodes))

0 commit comments

Comments
 (0)