09_rope.py 637 B

12345678910111213141516171819202122232425
  1. #!/usr/bin/env python3
  2. import sys
  3. def move(f):
  4. x = y = 0
  5. for line in f:
  6. direction, distance = line.split()
  7. for _ in range(int(distance)):
  8. x += (direction == 'R') - (direction == 'L')
  9. y += (direction == 'U') - (direction == 'D')
  10. yield x, y
  11. def follow(head):
  12. x = y = 0
  13. for hx, hy in head:
  14. if abs(hx - x) > 1 or abs(hy - y) > 1:
  15. y += (hy > y) - (hy < y)
  16. x += (hx > x) - (hx < x)
  17. yield x, y
  18. tenth = second = list(follow(move(sys.stdin)))
  19. for _ in range(8):
  20. tenth = follow(tenth)
  21. print(len(set(second)))
  22. print(len(set(tenth)))