Last active
December 10, 2024 12:14
-
-
Save rodrigogiraoserrao/bd3bd793b0953b666c35bafd4638400e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# === Parsing === | |
antennas = {} | |
with open("input.txt", "r") as f: | |
for y, line in enumerate(f): | |
for x, char in enumerate(line.strip()): | |
if char != ".": | |
antennas.setdefault(char, set()).add((x, y)) | |
max_x = x | |
max_y = y | |
print(antennas) | |
# === Part 1 === | |
from itertools import combinations | |
antinodes = set() | |
for antenna_group in antennas.values(): | |
for (x1, y1), (x2, y2) in combinations(antenna_group, 2): | |
dx, dy = x1 - x2, y1 - y2 | |
ax, ay = x1 + dx, y1 + dy | |
if 0 <= ax <= max_x and 0 <= ay <= max_y: | |
antinodes.add((ax, ay)) | |
ax, ay = x2 - dx, y2 - dy | |
if 0 <= ax <= max_x and 0 <= ay <= max_y: | |
antinodes.add((ax, ay)) | |
print(len(antinodes)) | |
# === Part 2 === | |
from itertools import combinations | |
antinodes = set() | |
for antenna_group in antennas.values(): | |
for (x1, y1), (x2, y2) in combinations(antenna_group, 2): | |
dx, dy = x1 - x2, y1 - y2 | |
step = 0 | |
while 0 <= x1 + step * dx <= max_x and 0 <= y1 + step * dy <= max_y: | |
antinodes.add((x1 + step * dx, y1 + step * dy)) | |
step += 1 | |
step = 0 | |
while 0 <= x1 - step * dx <= max_x and 0 <= y1 - step * dy <= max_y: | |
antinodes.add((x1 - step * dx, y1 - step * dy)) | |
step += 1 | |
print(len(antinodes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment