Skip to content

Commit 99f4e92

Browse files
committed
Day 8 part 2 success
1 parent 29e8ddf commit 99f4e92

File tree

2 files changed

+82
-9
lines changed

2 files changed

+82
-9
lines changed

2024/day_08/Python/antinodes.py

+45-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
from fractions import Fraction
12
import string
23
from itertools import combinations
34
import numpy as np
45

56

7+
def array_from_lines(lines):
8+
new_lines = []
9+
for line in lines:
10+
new_lines.append(np.array(list(line.strip())))
11+
12+
return np.array(new_lines)
13+
14+
615
def load_input(filename):
716
"""
817
Load the puzzle input from a file.
918
"""
1019
with open(filename, "r") as file:
1120
lines = file.readlines()
1221

13-
new_lines = []
14-
for line in lines:
15-
new_lines.append(np.array(list(line.strip())))
16-
17-
return np.array(new_lines)
22+
return array_from_lines(lines)
1823

1924

2025
def find_antennae(puzzle_input, freq):
@@ -37,9 +42,17 @@ def find_antinodes(coordinateA, coordinateB):
3742
return antinodeA, antinodeB
3843

3944

40-
if __name__ == "__main__":
41-
puzzle_input = load_input("input.txt")
45+
def find_antinodes_part_2(coordinateA, coordinateB):
46+
diff = coordinateA - coordinateB
47+
fr = Fraction(*diff)
48+
diff_simplified = np.array([fr.numerator, fr.denominator])
4249

50+
for i in range(100):
51+
yield coordinateA + i * diff_simplified
52+
yield coordinateA - i * diff_simplified
53+
54+
55+
def find_antinodes_from_antennas(find_antinodes_fn, puzzle_input):
4356
puzzle_bounds = np.array(puzzle_input).shape
4457

4558
frequencies = string.digits + string.ascii_lowercase + string.ascii_uppercase
@@ -51,17 +64,40 @@ def find_antinodes(coordinateA, coordinateB):
5164

5265
# Find antinodes
5366
for antenna in combinations(antennae, 2):
54-
antinodes.extend(find_antinodes(antenna[0], antenna[1]))
67+
antinodes.extend(find_antinodes_fn(antenna[0], antenna[1]))
5568

5669
antinodes = set(map(tuple, antinodes))
57-
print(antinodes)
5870

5971
# Remove out of bounds
6072
antinodes = [
6173
antinode
6274
for antinode in antinodes
6375
if all(0 <= coord < bound for coord, bound in zip(antinode, puzzle_bounds))
6476
]
77+
78+
puzzle_grid = np.array(puzzle_input)
79+
for antinode in antinodes:
80+
if puzzle_grid[antinode] == ".":
81+
puzzle_grid[antinode] = "#"
82+
83+
row_strs = []
84+
for row in puzzle_grid:
85+
row_strs.append("".join(row))
86+
87+
puzzle_grid_str = "\n".join(row_strs)
88+
89+
print(puzzle_grid_str)
90+
6591
print(len(antinodes))
6692

93+
return puzzle_grid_str
94+
95+
96+
if __name__ == "__main__":
97+
puzzle_input = load_input("input.txt")
98+
99+
find_antinodes_from_antennas(find_antinodes, puzzle_input)
100+
67101
# Part 2 here
102+
103+
find_antinodes_from_antennas(find_antinodes_part_2, puzzle_input)

2024/day_08/Python/test_antinodes.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import inspect
2+
from antinodes import (
3+
array_from_lines,
4+
find_antinodes_from_antennas,
5+
find_antinodes_part_2,
6+
)
7+
8+
9+
def test_antinodes_part_2():
10+
grid = inspect.cleandoc(
11+
"""
12+
.......
13+
.......
14+
....A..
15+
.......
16+
..A....
17+
.......
18+
.......
19+
"""
20+
).strip()
21+
22+
assert (
23+
inspect.cleandoc(
24+
"""
25+
......#
26+
.....#.
27+
....A..
28+
...#...
29+
..A....
30+
.#.....
31+
#......
32+
"""
33+
).strip()
34+
== find_antinodes_from_antennas(
35+
find_antinodes_part_2, array_from_lines(grid.split("\n"))
36+
)
37+
)

0 commit comments

Comments
 (0)