54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
|
#!/usr/bin/env python
|
||
|
# 2024 - Advent Of Code 8
|
||
|
|
||
|
# file = 'input_example.txt'
|
||
|
file = 'input.txt'
|
||
|
|
||
|
# pylint: disable=consider-using-with
|
||
|
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
|
||
|
|
||
|
MAX_X = len(input_lines[0])
|
||
|
MAX_Y = len(input_lines)
|
||
|
|
||
|
def find_antinode(s, dest):
|
||
|
print(f'from {s} considering {dest}')
|
||
|
res = []
|
||
|
diff_x = dest[0] - s[0]
|
||
|
diff_y = dest[1] - s[1]
|
||
|
# print(f'{diff_x}, {diff_y}')
|
||
|
|
||
|
p1 = (dest[0] + diff_x, dest[1] + diff_y)
|
||
|
if 0 <= p1[0] < MAX_X and 0 <= p1[1] < MAX_Y:
|
||
|
res.append(p1)
|
||
|
|
||
|
p2 = (s[0] - diff_x, s[1] - diff_y)
|
||
|
if 0 <= p2[0] < MAX_X and 0 <= p2[1] < MAX_Y:
|
||
|
res.append(p2)
|
||
|
|
||
|
return res
|
||
|
|
||
|
antennas = {}
|
||
|
freq = ''
|
||
|
for y in range(0, MAX_Y):
|
||
|
for x in range(0, MAX_X):
|
||
|
if input_lines[y][x] != '.':
|
||
|
freq = input_lines[y][x]
|
||
|
# print(f'found {freq=}')
|
||
|
if freq in antennas:
|
||
|
l = antennas[freq]
|
||
|
l.append((x, y))
|
||
|
else:
|
||
|
antennas[freq] = [(x, y)]
|
||
|
|
||
|
antinodes = set()
|
||
|
for freq, antennas_list in antennas.items():
|
||
|
print(f'\nfreq {freq} list: {antennas_list}')
|
||
|
for antenna in antennas_list:
|
||
|
for other in antennas_list:
|
||
|
if other != antenna:
|
||
|
for a in find_antinode(antenna, other):
|
||
|
antinodes.add(a)
|
||
|
|
||
|
print(f'{antinodes=}')
|
||
|
print(f'{len(antinodes)}')
|