08_antinodes.py 816 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env python3
  2. import sys
  3. from itertools import chain, permutations
  4. def parse(f):
  5. antennas = {}
  6. for y, line in enumerate(f):
  7. for x, char in enumerate(line[:-1]):
  8. if char != '.':
  9. antennas.setdefault(char, []).append((x, y))
  10. return antennas, x + 1, y + 1
  11. def subtract_pairs(antennas):
  12. for locs in antennas.values():
  13. for (xa, ya), (xb, yb) in permutations(locs, 2):
  14. yield xb, yb, xb - xa, yb - ya
  15. def draw_line(x, y, dx, dy, w, h):
  16. while 0 <= x < w and 0 <= y < h:
  17. yield x, y
  18. x += dx
  19. y += dy
  20. antennas, w, h = parse(sys.stdin)
  21. antinodes = [list(draw_line(*vec, w, h)) for vec in subtract_pairs(antennas)]
  22. print(len(set(a[1] for a in antinodes if len(a) > 1)))
  23. print(len(set(chain.from_iterable(antinodes))))