11_pebbles.py 752 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python3
  2. import sys
  3. from collections import Counter
  4. def change(stone):
  5. if stone == 0:
  6. yield 1
  7. else:
  8. digits = str(stone)
  9. mid, odd = divmod(len(digits), 2)
  10. if odd:
  11. yield stone * 2024
  12. else:
  13. yield int(digits[:mid])
  14. yield int(digits[mid:])
  15. def blink(counts, times):
  16. for _ in range(times):
  17. new = Counter()
  18. for stone, occurrences in counts.items():
  19. for newstone in change(stone):
  20. new[newstone] += occurrences
  21. counts = new
  22. return counts
  23. counts = Counter(map(int, next(sys.stdin).split()))
  24. counts = blink(counts, 25)
  25. print(sum(counts.values()))
  26. counts = blink(counts, 50)
  27. print(sum(counts.values()))