1
+ from fractions import Fraction
1
2
import string
2
3
from itertools import combinations
3
4
import numpy as np
4
5
5
6
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
+
6
15
def load_input (filename ):
7
16
"""
8
17
Load the puzzle input from a file.
9
18
"""
10
19
with open (filename , "r" ) as file :
11
20
lines = file .readlines ()
12
21
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 )
18
23
19
24
20
25
def find_antennae (puzzle_input , freq ):
@@ -37,9 +42,17 @@ def find_antinodes(coordinateA, coordinateB):
37
42
return antinodeA , antinodeB
38
43
39
44
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 ])
42
49
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 ):
43
56
puzzle_bounds = np .array (puzzle_input ).shape
44
57
45
58
frequencies = string .digits + string .ascii_lowercase + string .ascii_uppercase
@@ -51,17 +64,40 @@ def find_antinodes(coordinateA, coordinateB):
51
64
52
65
# Find antinodes
53
66
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 ]))
55
68
56
69
antinodes = set (map (tuple , antinodes ))
57
- print (antinodes )
58
70
59
71
# Remove out of bounds
60
72
antinodes = [
61
73
antinode
62
74
for antinode in antinodes
63
75
if all (0 <= coord < bound for coord , bound in zip (antinode , puzzle_bounds ))
64
76
]
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
+
65
91
print (len (antinodes ))
66
92
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
+
67
101
# Part 2 here
102
+
103
+ find_antinodes_from_antennas (find_antinodes_part_2 , puzzle_input )
0 commit comments