Compare commits

...

2 Commits

Author SHA1 Message Date
0x01fe
a5f37defce just some clean up stuff 2024-12-08 14:39:16 -06:00
0x01fe
465aaf8b24 solved 2024 day 8 part 2 2024-12-08 14:38:41 -06:00
4 changed files with 51 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

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}'
@ -27,5 +30,3 @@ class Vector:
def distance(self, other: Self) -> int:
return math.sqrt((other.x - self.x)**2 + (other.y - self.y)**2)