15_numbersgame.py 453 B

12345678910111213141516171819
  1. #!/usr/bin/env python3
  2. def gen(start):
  3. yield from start
  4. spoken = {n: i for i, n in enumerate(start)}
  5. i = len(start)
  6. num = 0
  7. while True:
  8. yield num
  9. nextnum = i - spoken.get(num, i)
  10. spoken[num] = i
  11. num = nextnum
  12. i += 1
  13. def nth(start, n):
  14. return next(num for i, num in enumerate(gen(start)) if i == n - 1)
  15. start = [13, 0, 10, 12, 1, 5, 8]
  16. print(nth(start, 2020))
  17. print(nth(start, 30000000))