solved part 1 day8 2024
This commit is contained in:
1 parent
520aa782da
commit
e5f3e9015f
4 files changed
+141
No files matched your search
@@ -0,0 +1,48 @@
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user