2024-12-08 14:38:41 -06:00

78 lines
2.0 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.strip()):
if char not in ['.']:
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
def print_nodes(antinodes: list[Vector]) -> None:
datac = data.copy()
for antinode in antinodes:
datac[antinode.y] = datac[antinode.y][:antinode.x] + '#' + datac[antinode.y][antinode.x + 1:]
for line in datac:
print(line, end='')
print()
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)
print_nodes(antinodes)
print(f'Part 1: {len(antinodes)}')
# Part 2
antinodes = []
for char, group in groups.items():
for i, A in enumerate(group):
for B in group[i + 1:]:
dist = B - A
antinode_pos = A + dist
while in_bounds(antinode_pos):
if antinode_pos not in antinodes:
antinodes.append(antinode_pos)
antinode_pos += dist
dist = dist.inverse()
antinode_pos = B + dist
while in_bounds(antinode_pos):
if antinode_pos not in antinodes:
antinodes.append(antinode_pos)
antinode_pos += dist
print_nodes(antinodes)
print(f'Part 2: {len(antinodes)}')