advent-of-code/2024/8/two.py
2024-12-25 12:59:49 +01:00

50 lines
1.3 KiB
Python

#!/usr/bin/env python3
import sys
input_file = sys.argv[1]
with open(input_file) as fd:
lines = [line.rstrip() for line in fd.readlines()]
height = len(lines)
width = len(lines[0])
antennas: dict[str, list[tuple[int, int]]] = dict()
viz = [["."] * width for _ in range(height)]
for i in range(height):
for j in range(width):
char = lines[i][j]
if char == ".":
continue
antennas.setdefault(char, list())
antennas[char].append((i, j))
viz[i][j] = char
antinodes_locations: set[tuple[int, int]] = set()
for char, char_antennas in antennas.items():
print(f"25 {char} {char_antennas}")
for ant_a in char_antennas:
for ant_b in char_antennas:
if ant_a == ant_b:
continue
m = 0
while True:
i, j = ant_b[0] + m * (ant_b[0] - ant_a[0]), ant_b[1] + m * (
ant_b[1] - ant_a[1]
)
antinode_loc = i, j
print(f"30 {antinode_loc}")
if i not in range(height) or j not in range(width):
break
print("kept")
antinodes_locations.add(antinode_loc)
viz[i][j] = "#"
m += 1
for vline in viz:
print("".join(vline))
print(len(antinodes_locations))