14_robots.py 960 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python3
  2. import sys
  3. import re
  4. from itertools import islice
  5. W = 101
  6. H = 103
  7. def walk(robots):
  8. positions, velocities = zip(*((r[:2], r[2:]) for r in robots))
  9. while True:
  10. positions = [((x + dx) % W, (y + dy) % H)
  11. for (x, y), (dx, dy) in zip(positions, velocities)]
  12. yield positions
  13. def safety(positions):
  14. quadrants = [0, 0, 0, 0]
  15. midx = W // 2
  16. midy = H // 2
  17. for x, y in positions:
  18. if x != midx and y != midy:
  19. quadrants[(x > midx) + 2 * (y > midy)] += 1
  20. a, b, c, d = quadrants
  21. return a * b * c * d
  22. positions = walk(tuple(map(int, re.findall(r'-?\d+', line)))
  23. for line in sys.stdin)
  24. for step in range(1, 101):
  25. pos = next(positions)
  26. print(safety(pos))
  27. s = ''
  28. while 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' not in s:
  29. pos = set(next(positions))
  30. s = ''.join('.X'[(x, y) in pos] for y in range(H) for x in range(W))
  31. step += 1
  32. print(step)