2019 day 22 part 1
This commit is contained in:
parent
695a4f7ddb
commit
36f20610ae
53
2019/d22.py
Normal file
53
2019/d22.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
from lib import get_data, str_to_ints, mod_inverse
|
||||||
|
from math import gcd
|
||||||
|
|
||||||
|
data = get_data(__file__)
|
||||||
|
deck = list(range(10007))
|
||||||
|
|
||||||
|
for line in data.splitlines():
|
||||||
|
if "new stack" in line:
|
||||||
|
deck = list(reversed(deck))
|
||||||
|
elif "cut" in line:
|
||||||
|
n, = str_to_ints(line)
|
||||||
|
deck = deck[n:] + deck[:n]
|
||||||
|
elif "increment" in line:
|
||||||
|
new_deck = [-1] * len(deck)
|
||||||
|
pos = 0
|
||||||
|
n, = str_to_ints(line)
|
||||||
|
deck = list(reversed(deck))
|
||||||
|
while deck:
|
||||||
|
new_deck[pos] = deck.pop()
|
||||||
|
pos = (pos + n) % len(new_deck)
|
||||||
|
deck = new_deck
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
|
||||||
|
print(deck.index(2019))
|
||||||
|
|
||||||
|
# idea: just follow the index of the target card
|
||||||
|
len = 119315717514047
|
||||||
|
len = 10007
|
||||||
|
index = 2019
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(101741582076661):
|
||||||
|
for line in data.splitlines():
|
||||||
|
if "new stack" in line:
|
||||||
|
index = len - (index + 1)
|
||||||
|
elif "cut" in line:
|
||||||
|
cut, = str_to_ints(line)
|
||||||
|
cut = (len + cut) % len
|
||||||
|
if index >= cut:
|
||||||
|
index = index - cut
|
||||||
|
else:
|
||||||
|
index = (len - cut) + index
|
||||||
|
elif "increment" in line:
|
||||||
|
n, = str_to_ints(line)
|
||||||
|
assert gcd(n, len) == 1
|
||||||
|
new_index = (n * index) % len
|
||||||
|
m = mod_inverse(n, len)
|
||||||
|
assert (new_index * m) % len == index
|
||||||
|
index = new_index
|
||||||
|
break
|
||||||
|
|
||||||
|
print(index)
|
Loading…
Reference in New Issue
Block a user