/ Python / 2020 / 23.py
23.py
 1  from lib import *
 2  
 3  input = read_input(2020, 23)
 4  
 5  
 6  def move1(state):
 7      current, moving, suffix = state[0], state[1:4], state[4:]
 8      dst = suffix.index(max([x for x in suffix if int(x) < int(current)], default=max(suffix)))
 9      state = current + suffix[: dst + 1] + moving + suffix[dst + 1 :]
10      return state[1:] + state[0]
11  
12  
13  state = input.strip()
14  for _ in range(100):
15      state = move1(state)
16  one = state.index("1")
17  print(state[one + 1 :] + state[:one])
18  
19  
20  def move2(state, current):
21      first = state[current]
22      second = state[first]
23      third = state[second]
24      suffix = state[third]
25      dst = current
26      while dst in (current, first, second, third):
27          dst -= 1
28          if not dst:
29              dst = len(state) - 1
30  
31      state[current] = suffix
32      state[third] = state[dst]
33      state[dst] = first
34      return state[current]
35  
36  
37  nums = list(map(int, input.strip()))
38  state = list(range(1, 1000002))
39  for a, b in zip([-1] + nums, nums + [len(nums) + 1]):
40      state[a] = b
41  
42  current = nums[0]
43  for _ in range(10000000):
44      current = move2(state, current)
45  first = state[1]
46  second = state[first]
47  print(first * second)