11_pebbles.py 727 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. s = str(stone)
  9. l = len(s)
  10. if l % 2 == 0:
  11. yield int(s[:l // 2])
  12. yield int(s[l // 2:])
  13. else:
  14. yield stone * 2024
  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()))