1
+ import string
2
+ from itertools import combinations
3
+ import numpy as np
4
+
5
+ def load_input (filename ):
6
+ """
7
+ Load the puzzle input from a file.
8
+ """
9
+ with open (filename , 'r' ) as file :
10
+ lines = file .readlines ()
11
+
12
+ new_lines = []
13
+ for line in lines :
14
+ new_lines .append (np .array (list (line .strip ())))
15
+
16
+ return np .array (new_lines )
17
+
18
+
19
+ def find_antennae (puzzle_input , freq ):
20
+ """
21
+ Find the antennae, for this frequency, in the puzzle input.
22
+ """
23
+
24
+ return np .argwhere (puzzle_input == freq )
25
+
26
+
27
+ def find_antinodes (coordinateA , coordinateB ):
28
+ """
29
+ Find the antinodes for the given antenna in the puzzle input.
30
+ """
31
+ # Difference between the two antennae
32
+ diff = coordinateA - coordinateB
33
+ antinodeA = coordinateA + diff
34
+ antinodeB = coordinateB - diff
35
+
36
+ return antinodeA , antinodeB
37
+
38
+ if __name__ == "__main__" :
39
+ puzzle_input = load_input ("input.txt" )
40
+
41
+ puzzle_bounds = np .array (puzzle_input ).shape
42
+
43
+ frequencies = string .digits + string .ascii_lowercase + string .ascii_uppercase
44
+
45
+ antinodes = []
46
+ for freq in frequencies :
47
+ # Find antennae
48
+ antennae = find_antennae (puzzle_input , freq )
49
+
50
+ # Find antinodes
51
+ for antenna in combinations (antennae , 2 ):
52
+ antinodes .extend (find_antinodes (antenna [0 ], antenna [1 ]))
53
+
54
+
55
+ antinodes = set (map (tuple , antinodes ))
56
+ print (antinodes )
57
+
58
+ # Remove out of bounds
59
+ antinodes = [
60
+ antinode for antinode in antinodes if all (0 <= coord < bound for coord , bound in zip (antinode , puzzle_bounds ))
61
+ ]
62
+ print (len (antinodes ))
63
+
64
+ # Part 2 here
0 commit comments