-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.py
40 lines (33 loc) · 1.05 KB
/
22.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from lib import *
input = read_input(2019, 22)
DECK = 10007
pos = 2019
for line in input.splitlines():
reverse = re.match(r"deal into new stack", line)
cut = re.match(r"cut (-?\d+)", line)
increment = re.match(r"deal with increment (\d+)", line)
if reverse:
pos = -pos - 1
elif cut:
pos -= int(cut.group(1))
elif increment:
pos *= int(increment.group(1))
pos %= DECK
print(pos)
DECK = 119315717514047
REP = 101741582076661
first, inc = 0, 1
instructions = input.splitlines()
for line in instructions:
reverse = re.match(r"deal into new stack", line)
cut = re.match(r"cut (-?\d+)", line)
increment = re.match(r"deal with increment (\d+)", line)
if reverse:
add, mul = -1, -1
elif cut:
add, mul = int(cut.group(1)), 1
elif increment:
add, mul = 0, pow(int(increment.group(1)), DECK - 2, DECK)
first, inc = first + add * inc, inc * mul
first, inc = first * (1 - pow(inc, REP, DECK)) * pow(1 - inc, DECK - 2, DECK), pow(inc, REP, DECK)
print((2020 * inc + first) % DECK)