2024-12-08 14:11:31 -06:00

49 lines
1.2 KiB
Python

from vector import Vector
with open('input.text', 'r') as file:
data: list[str] = file.readlines()
groups = {}
# Parse Data
y_bound: int = len(data)
x_bound: int = len(data[0].strip())
for y, line in enumerate(data):
for x, char in enumerate(line):
if char not in ['.', '\n']:
if char not in groups:
groups[char] = [Vector(x, y)]
else:
groups[char].append(Vector(x, y))
# Part 1
def in_bounds(v: Vector) -> bool:
return v.x >= 0 and v.x < x_bound and v.y >= 0 and v.y < y_bound
antinodes = []
for char, group in groups.items():
for i, A in enumerate(group):
for B in group[i + 1:]:
antinode_pos = A + A - B
antinode_pos2 = B + B - A
if antinode_pos not in antinodes and in_bounds(antinode_pos):
antinodes.append(antinode_pos)
if antinode_pos2 not in antinodes and in_bounds(antinode_pos2):
antinodes.append(antinode_pos2)
for antinode in antinodes:
data[antinode.y] = data[antinode.y][:antinode.x] + '#' + data[antinode.y][antinode.x + 1:]
for line in data:
print(line, end='')
print(f'\nPart 1: {len(antinodes)}')
# Part 2