You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.4 KiB
44 lines
1.4 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
from itertools import combinations
|
|
from copy import deepcopy
|
|
|
|
|
|
if __name__ == '__main__':
|
|
mp = [list(line.strip('\n')) for line in open(sys.argv[1])]
|
|
antinodes = deepcopy(mp)
|
|
ah = deepcopy(mp)
|
|
|
|
antennas = {}
|
|
for j, y in enumerate(mp):
|
|
for i, x in enumerate(y):
|
|
if x != '.':
|
|
if x in antennas: antennas[x].append((i,j))
|
|
else: antennas[x] = [(i,j)]
|
|
ah[j][i] = '#'
|
|
|
|
for freq, ants in antennas.items():
|
|
for a1, a2 in combinations(ants, 2):
|
|
dx, dy = a2[0] - a1[0], a2[1] - a1[1]
|
|
x1, y1 = a1[0] - dx, a1[1] - dy
|
|
x2, y2 = a2[0] + dx, a2[1] + dy
|
|
if (0 <= x1 < len(mp[0])) and (0 <= y1 < len(mp)):
|
|
antinodes[y1][x1] = '#'
|
|
if (0 <= x2 < len(mp[0])) and (0 <= y2 < len(mp)):
|
|
antinodes[y2][x2] = '#'
|
|
while (0 <= x1 < len(mp[0])) and (0 <= y1 < len(mp)):
|
|
ah[y1][x1] = '#'
|
|
x1, y1 = x1-dx, y1-dy
|
|
while (0 <= x2 < len(mp[0])) and (0 <= y2 < len(mp)):
|
|
ah[y2][x2] = '#'
|
|
x2, y2 = x2+dx, y2+dy
|
|
|
|
# challenge 1
|
|
res1 = sum([a.count('#') for a in antinodes])
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
res2 = sum([a.count('#') for a in ah])
|
|
print(f"challenge 2:\n{res2}")
|
|
|
|
|