summaryrefslogtreecommitdiff
path: root/2024/day08/solve2.py
blob: 1642e35d87e31247753e991223b495af86c1a8fa (plain)
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
lines = []

with open("input") as f:
    for line in f:
        lines.append(line.strip())

antennas: dict[str, list[tuple[int, int]]] = {}
for row_idx, line in enumerate(lines):
    for col_idx, char in enumerate(line):
        if char != ".":
            if char not in antennas:
                antennas[char] = []

            antennas[char].append((row_idx, col_idx))

row_count = len(lines)
col_count = len(lines[0])
antinodes: set[tuple[int, int]] = set()
for antenna_list in antennas.values():
    for idx, antenna1 in enumerate(antenna_list):
        for antenna2 in antenna_list[idx + 1:]:
            row_diff = antenna2[0] - antenna1[0]
            col_diff = antenna2[1] - antenna1[1]

            pos = antenna1
            while 0 <= pos[0] < row_count and 0 <= pos[1] < col_count:
                antinodes.add(pos)
                pos = (pos[0] - row_diff, pos[1] - col_diff)

            pos = antenna2
            while 0 <= pos[0] < row_count and 0 <= pos[1] < col_count:
                antinodes.add(pos)
                pos = (pos[0] + row_diff, pos[1] + col_diff)

print(len(antinodes))