-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday22.py
executable file
·89 lines (68 loc) · 1.55 KB
/
day22.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
from utils.all import *
fin = advent.get_input()
# eprint(*fin, sep='')
timer_start()
##################################################
lines = get_lines(fin)
import re
from math import gcd
rexp = re.compile(r'-?\d+')
DEAL_STEP, DEAL_NEW, CUT = 0,1,2
def build_moves(length):
moves = []
for l in lines:
if 'deal with' in l:
n = int(rexp.findall(l)[0])
moves.append((DEAL_STEP, n))
elif 'deal into' in l:
moves.append((DEAL_NEW, 0))
elif 'cut' in l:
n = int(rexp.findall(l)[0])
if n < 0:
n += length
moves.append((CUT, n))
return moves
def shuffle(i, length, moves):
for move, n in moves:
if move == DEAL_NEW:
i = length - i - 1
elif move == DEAL_STEP:
i = (i * n) % length
else:
if i >= n:
i -= n
else:
i = length - n + i
return i
def shuffle2(a, b, length, moves):
for move, n in moves:
if move == DEAL_NEW:
a = (-1 * a) % length
b = (b + a) % length
elif move == DEAL_STEP:
a = (a * pow(n, length-2, length)) % length
else:
b = (a * n + b) % length
return a, b
card = 2019
length = 10007
moves = build_moves(length)
card = shuffle(card, length, moves)
advent.submit_answer(1, card)
card = 2020
length = 119315717514047
repetitions = 101741582076661
moves = build_moves(length)
a = 1
b = 0
a, b = shuffle2(a, b, length, moves)
# print(a, b)
A = pow(a, repetitions, length)
S = (1 - A) * pow(1 - a, length-2, length)
B = (b * S) % length
# print(A, B)
final = (A * card + B) % length
# 56945290822617 wrong
# 106845040107460 wrong
advent.submit_answer(2, final)