-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2220.py
56 lines (49 loc) · 1.38 KB
/
aoc2220.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
from typing import List
import copy
def main():
file = open('2220.1')
res, res2 = day22(file)
print('test')
print(f'Star 1: {res} \nStar 2: {res2}\n')
file = open('2220.0')
res, res2 = day22(file)
print('data')
print(f'Star 1: {res} \nStar 2: {res2}')
def day22(file) -> (int, int):
line = file.read().strip().split()
a = [int(_) for _ in line]
idx = [i for i in range(0, len(a))]
aa = copy.deepcopy(a)
idxx = copy.deepcopy(idx)
yield solve(a, idx, 1, 1)
p2 = 811589153
yield solve(aa, idxx, p2, 10)
def solve(a: List[int], idx: List[int], key: int, times: int) -> int:
for i in range(len(a)):
a[i] *= key
res = 0
for t in range(times):
for i in range(len(a)):
if i not in idx:
return 0
index = idx.index(i)
value = a.pop(index)
N = index + value
S = len(a)
move2 = N % S
if move2 == 0 and value != 0:
move2 = len(a)
elif move2 == len(a):
move2 = 0
a.insert(move2, value)
idx.pop(index)
idx.insert(move2, i)
i = a.index(0)
S = len(a)
th = 1000
res = a[(th + i) % S] \
+ a[(th*2+i) % S] \
+ a[(th*3+i) % S]
return res
if __name__ == '__main__':
main()