|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +@dataclass |
| 4 | +class Location: |
| 5 | + frequency: str = None |
| 6 | + is_antinode: bool = False |
| 7 | + position: tuple = (-1, -1) |
| 8 | + def __repr__(self): |
| 9 | + if self.frequency is not None: |
| 10 | + return self.frequency |
| 11 | + if self.is_antinode: |
| 12 | + return '#' |
| 13 | + return '.' |
| 14 | + |
| 15 | +map = [] |
| 16 | + |
| 17 | +with open('input.txt', 'r') as file: |
| 18 | + for y, line in enumerate(file): |
| 19 | + map_row = [] |
| 20 | + for x, char in enumerate(line.strip()): |
| 21 | + location = Location() |
| 22 | + if char != '.': |
| 23 | + location.frequency = char |
| 24 | + location.position = (x, y) |
| 25 | + map_row.append(location) |
| 26 | + map.append(map_row) |
| 27 | + |
| 28 | +antennas = [] |
| 29 | +for row in map: |
| 30 | + for location in row: |
| 31 | + if location.frequency is not None: |
| 32 | + antennas.append(location) |
| 33 | + |
| 34 | +def get_line_equation(location1, location2): |
| 35 | + x1, y1 = location1.position |
| 36 | + x2, y2 = location2.position |
| 37 | + if x1 == x2: |
| 38 | + return None |
| 39 | + m = (y2 - y1) / (x2 - x1) |
| 40 | + b = y1 - m * x1 |
| 41 | + return m, b |
| 42 | + |
| 43 | +def is_on_map(x, y): |
| 44 | + return x >= 0 and x < len(map[0]) and y >= 0 and y < len(map) |
| 45 | + |
| 46 | +# Part 1 |
| 47 | + |
| 48 | +for antenna in antennas: |
| 49 | + for other_antenna in [other_antenna for other_antenna in antennas if other_antenna != antenna]: |
| 50 | + if antenna.frequency == other_antenna.frequency: |
| 51 | + line_equation = get_line_equation(antenna, other_antenna) |
| 52 | + antinode1_x = antinode1_y = antinode2_x = antinode2_y = None |
| 53 | + [x1, x2] = ([antenna.position[0], other_antenna.position[0]]) |
| 54 | + x_diff = x2 - x1 |
| 55 | + antinode1_x = x1 - x_diff |
| 56 | + antinode2_x = x2 + x_diff |
| 57 | + m, b = line_equation |
| 58 | + antinode1_y = round(m * antinode1_x + b) |
| 59 | + antinode2_y = round(m * antinode2_x + b) |
| 60 | + if is_on_map(antinode1_x, antinode1_y): |
| 61 | + map[antinode1_y][antinode1_x].is_antinode = True |
| 62 | + if is_on_map(antinode2_x, antinode2_y): |
| 63 | + map[int(antinode2_y)][antinode2_x].is_antinode = True |
| 64 | + |
| 65 | +antinode_count = len([location for row in map for location in row if location.is_antinode]) |
| 66 | + |
| 67 | +print('Number of antinodes (part 1):', antinode_count) |
| 68 | + |
| 69 | +# Part 2 |
| 70 | + |
| 71 | +for antenna in antennas: |
| 72 | + for other_antenna in [other_antenna for other_antenna in antennas if other_antenna != antenna]: |
| 73 | + if antenna.frequency == other_antenna.frequency: |
| 74 | + x_diff = abs(antenna.position[0] - other_antenna.position[0]) |
| 75 | + line_equation = get_line_equation(antenna, other_antenna) |
| 76 | + x_values = ( |
| 77 | + [x for x in range(antenna.position[0], -1, -x_diff)] |
| 78 | + + [x for x in range(antenna.position[0], len(map[0]), x_diff)] |
| 79 | + ) |
| 80 | + for x in x_values: |
| 81 | + m, b = line_equation |
| 82 | + y = round(m * x + b) |
| 83 | + if is_on_map(x, y): |
| 84 | + map[y][x].is_antinode = True |
| 85 | + |
| 86 | +antinode_count = len([location for row in map for location in row if location.is_antinode]) |
| 87 | + |
| 88 | +print('Number of antinodes (part 2):', antinode_count) |
0 commit comments