Taddeus Kroes 5 anni fa
parent
commit
3d5759c13f
1 ha cambiato i file con 35 aggiunte e 0 eliminazioni
  1. 35 0
      2020/23_cups.py

+ 35 - 0
2020/23_cups.py

@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+def move(cups, moves, pad):
+    nex = [i + 1 for i in range(pad + 1)]
+    for i, label in enumerate(cups[:-1]):
+        nex[label] = cups[i + 1]
+    head = cups[0]
+    if pad > len(cups):
+        nex[-1] = head
+        nex[cups[-1]] = max(cups) + 1
+    else:
+        nex[cups[-1]] = head
+
+    for i in range(moves):
+        rem = nex[head]
+        nex[head] = nex[nex[nex[rem]]]
+        allrem = rem, nex[rem], nex[nex[rem]]
+
+        dest = head - 1 if head > 1 else pad
+        while dest in allrem:
+            dest = pad if dest == 1 else dest - 1
+
+        nex[nex[nex[rem]]] = nex[dest]
+        nex[dest] = rem
+
+        head = nex[head]
+
+    cup = nex[1]
+    while cup != 1:
+        yield cup
+        cup = nex[cup]
+
+cups = list(map(int, '925176834'))
+print(''.join(map(str, move(cups, 100, len(cups)))))
+m = move(cups, 10000000, 1000000)
+print(next(m) * next(m))