No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

42 líneas
1.0KB

  1. import itertools
  2. import math
  3. import sys
  4. from functools import partial
  5. def measure(start, other):
  6. dx, dy = other[0] - start[0], other[1] - start[1]
  7. angle = math.atan2(-dx, dy)
  8. return -math.pi if angle == math.pi else angle, math.hypot(dx, dy), other
  9. def seek(start, asteroids):
  10. pending = sorted(measure(start, ast) for ast in asteroids)
  11. while pending:
  12. copy = pending.copy()
  13. pending = []
  14. out = {}
  15. for angle, dist, ast in copy:
  16. if angle in out:
  17. pending.append((angle, dist, ast))
  18. else:
  19. out[angle] = ast
  20. yield list(out.values())
  21. text = sys.stdin.read()
  22. asteroids = {
  23. (X, Y)
  24. for Y, line in enumerate(text.splitlines())
  25. for X, cell in enumerate(line)
  26. if cell == '#'
  27. }
  28. visibility = {ast: len(next(seek(ast, asteroids))) for ast in asteroids}
  29. origin = max(visibility, key=visibility.get)
  30. print(visibility[origin])
  31. ordering = list(itertools.chain(*seek(origin, asteroids)))
  32. x, y = ordering[200 - 1]
  33. print(100 * x + y)