23.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from collections import deque
  2. from time import time
  3. input = "137826495"
  4. #input = "389125467"
  5. cups = [int(i) for i in list(input)]
  6. max_cup = max(cups)
  7. def my_popleft(cups, following):
  8. current = cups.popleft()
  9. if current in following:
  10. cups.extendleft(following[current])
  11. del following[current]
  12. return current
  13. def run_on_cups(cups, iterations):
  14. cup_count = len(cups)
  15. max_cup = max(cups)
  16. following = {}
  17. start = time()
  18. print("Start")
  19. for i in range(0, iterations):
  20. current_cup = my_popleft(cups, following)
  21. one = my_popleft(cups, following)
  22. two = my_popleft(cups, following)
  23. three = my_popleft(cups, following)
  24. three_cups = [three, two, one]
  25. dest = current_cup
  26. while True:
  27. dest -= 1
  28. if dest == 0:
  29. dest = max_cup
  30. if dest not in three_cups:
  31. break
  32. following[dest] = three_cups
  33. cups.append(current_cup)
  34. print("Time:", time() - start)
  35. while len(following) > 0:
  36. current = my_popleft(cups, following)
  37. cups.append(current)
  38. return cups
  39. res1 = run_on_cups(deque(cups), 100)
  40. print("Answer 1:", ''.join([str(i) for i in res1]))
  41. res2 = run_on_cups(deque(cups + list(range(10, 1000001))), 10000000)
  42. res2.rotate(- res2.index(1))
  43. assert res2.popleft() == 1
  44. a = res2.popleft()
  45. b = res2.popleft()
  46. print("Answer 2:", a * b)