Taddeus Kroes před 6 roky
rodič
revize
dfbd9b66c6
2 změnil soubory, kde provedl 143 přidání a 0 odebrání
  1. 43 0
      2019/22_spacecards.py
  2. 100 0
      2019/input/22

+ 43 - 0
2019/22_spacecards.py

@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+import sys
+from functools import partial, reduce
+
+# get (a, b) coefficients for inverse linear index mapping functions ax+b
+def inverse_functions(instructions, ncards):
+    for line in reversed(instructions):
+        words = line.split()
+        if words[0] == 'cut':
+            yield 1, int(words[1])
+        elif words[1] == 'into':
+            yield -1, ncards - 1
+        else:
+            increment = int(words[-1])
+            index_increment = pow(increment, ncards - 2, ncards)
+            yield index_increment, 0
+
+# compose linear functions ax+b and cx+d
+def compose(mod, f, g):
+    a, b = f
+    c, d = g
+    return c * a % mod, (c * b + d) % mod
+
+# repeat ax+b n times
+def repeat_linear(f, n, mod):
+    if n == 0:
+        return 1, 0
+    if n == 1:
+        return f
+    half, odd = divmod(n, 2)
+    g = repeat_linear(f, half, mod)
+    g = compose(mod, g, g)
+    return compose(mod, f, g) if odd else g
+
+def card_at(index, ncards, nshuffles, instructions):
+    functions = inverse_functions(instructions, ncards)
+    invmap = reduce(partial(compose, ncards), functions, (1, 0))
+    a, b = repeat_linear(invmap, nshuffles, ncards)
+    return (index * a + b) % ncards
+
+instructions = list(sys.stdin)
+print(next(i for i in range(10007) if card_at(i, 10007, 1, instructions) == 2019))
+print(card_at(2020, 119315717514047, 101741582076661, instructions))

+ 100 - 0
2019/input/22

@@ -0,0 +1,100 @@
+deal with increment 55
+cut -6791
+deal with increment 9
+cut -5412
+deal with increment 21
+deal into new stack
+deal with increment 72
+cut -362
+deal with increment 24
+cut -5369
+deal with increment 22
+cut 731
+deal with increment 72
+cut 412
+deal into new stack
+deal with increment 22
+cut -5253
+deal with increment 73
+deal into new stack
+cut -6041
+deal into new stack
+cut 6605
+deal with increment 6
+cut 9897
+deal with increment 59
+cut -9855
+deal into new stack
+cut -7284
+deal with increment 7
+cut 332
+deal with increment 37
+deal into new stack
+deal with increment 43
+deal into new stack
+deal with increment 59
+cut 1940
+deal with increment 16
+cut 3464
+deal with increment 24
+cut -7766
+deal with increment 36
+cut -156
+deal with increment 18
+cut 8207
+deal with increment 33
+cut -393
+deal with increment 4
+deal into new stack
+cut -4002
+deal into new stack
+cut -8343
+deal into new stack
+deal with increment 70
+deal into new stack
+cut 995
+deal with increment 22
+cut 1267
+deal with increment 47
+cut -3161
+deal into new stack
+deal with increment 34
+cut -6221
+deal with increment 26
+cut 4956
+deal with increment 57
+deal into new stack
+cut -4983
+deal with increment 36
+cut -1101
+deal into new stack
+deal with increment 2
+cut 4225
+deal with increment 35
+cut -721
+deal with increment 17
+cut 5866
+deal with increment 40
+cut -531
+deal into new stack
+deal with increment 63
+cut -5839
+deal with increment 30
+cut 5812
+deal with increment 35
+deal into new stack
+deal with increment 46
+cut -5638
+deal with increment 60
+deal into new stack
+deal with increment 33
+cut -4690
+deal with increment 7
+cut 6264
+deal into new stack
+cut 8949
+deal into new stack
+cut -4329
+deal with increment 52
+cut 3461
+deal with increment 47