23.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. input = "137826495"
  2. #input = "389125467"
  3. cups = [int(i) for i in list(input)]
  4. max_cup = max(cups)
  5. def ring_str(after):
  6. result = ""
  7. current = 1
  8. printed = []
  9. while current not in printed:
  10. result += str(current)
  11. printed.append(current)
  12. current = after[current]
  13. return result
  14. def run_on_cups(cups, iterations):
  15. cup_count = len(cups)
  16. max_cup = max(cups)
  17. after = {}
  18. for i in range(0, cup_count - 1):
  19. after[cups[i]] = cups[i + 1]
  20. after[cups[-1]] = cups[0]
  21. current_cup = cups[0]
  22. for i in range(0, iterations):
  23. #print("Current:", current_cup)
  24. #print_ring(after)
  25. one = after[current_cup]
  26. two = after[one]
  27. three = after[two]
  28. three_cups = [one, two, three]
  29. #print("Selecting:", three_cups)
  30. dest = current_cup
  31. while True:
  32. dest -= 1
  33. if dest == 0:
  34. dest = max_cup
  35. if dest not in three_cups:
  36. break
  37. #print("Destination:", dest)
  38. after[current_cup] = after[three]
  39. after[three] = after[dest]
  40. after[dest] = one
  41. current_cup = after[current_cup]
  42. return after
  43. after = run_on_cups(cups, 100)
  44. print("Answer 1:", ring_str(after)[1:])
  45. after = run_on_cups(cups + list(range(10, 1000001)), 10000000)
  46. a = after[1]
  47. b = after[a]
  48. print("Answer 2:", a * b)