solved 2024 day 8 part 2
This commit is contained in:
1 parent
e5f3e9015f
commit
465aaf8b24
3 files changed
+50
-8
No files matched your search
+37
-8
@@ -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)}')
|
||||
Reference in new issue
Block a user