23_cups.py 891 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python3
  2. def move(cups, moves, pad):
  3. nex = [i + 1 for i in range(pad + 1)]
  4. for i, label in enumerate(cups[:-1]):
  5. nex[label] = cups[i + 1]
  6. head = cups[0]
  7. if pad > len(cups):
  8. nex[-1] = head
  9. nex[cups[-1]] = max(cups) + 1
  10. else:
  11. nex[cups[-1]] = head
  12. for i in range(moves):
  13. rem = nex[head]
  14. nex[head] = nex[nex[nex[rem]]]
  15. allrem = rem, nex[rem], nex[nex[rem]]
  16. dest = head - 1 if head > 1 else pad
  17. while dest in allrem:
  18. dest = pad if dest == 1 else dest - 1
  19. nex[nex[nex[rem]]] = nex[dest]
  20. nex[dest] = rem
  21. head = nex[head]
  22. cup = nex[1]
  23. while cup != 1:
  24. yield cup
  25. cup = nex[cup]
  26. cups = list(map(int, '925176834'))
  27. print(''.join(map(str, move(cups, 100, len(cups)))))
  28. m = move(cups, 10000000, 1000000)
  29. print(next(m) * next(m))