solved 2024 day 8 part 2

This commit is contained in:
0x01fe 2024-12-08 14:38:41 -06:00
parent e5f3e9015f
commit 465aaf8b24
3 changed files with 50 additions and 8 deletions

View File

@ -11,8 +11,8 @@ 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']:
for x, char in enumerate(line.strip()):
if char not in ['.']:
if char not in groups:
groups[char] = [Vector(x, y)]
else:
@ -22,6 +22,15 @@ for y, line in enumerate(data):
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):
@ -36,13 +45,33 @@ for char, group in groups.items():
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:]
print_nodes(antinodes)
for line in data:
print(line, end='')
print(f'\nPart 1: {len(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)}')

10
2024/day8/test2.text Normal file
View File

@ -0,0 +1,10 @@
T.........
...T......
.T........
..........
..........
..........
..........
..........
..........
..........

View File

@ -19,6 +19,9 @@ class Vector:
def abs(self) -> Self:
return Vector(abs(self.x), abs(self.y))
def inverse(self) -> Self:
return Vector(-self.x, -self.y)
def __str__(self) -> str:
return f'{self.x}, {self.y}'