-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdeque.py
executable file
·44 lines (31 loc) · 1.08 KB
/
deque.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
#!/usr/bin/env python3
#
# This solution should *in theory* be faster. However, given the amount of calls
# to .index() and .rotate(), and the nature of deques, it is actually slightly
# slower than using "nurmal" lists. It is *a lot* slower with PyPy.
#
import sys
from collections import deque
def mix(order, numbers, times=1):
sz = len(numbers)
for _ in range(times):
for item in order:
numbers.rotate(-numbers.index(item))
numbers.popleft()
numbers.rotate(-item[1])
numbers.appendleft(item)
for i, item in enumerate(numbers):
if item[1] == 0:
break
return numbers[(i + 1000) % sz][1] + numbers[(i + 2000) % sz][1] + numbers[(i + 3000) % sz][1]
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
with fin:
order = tuple((i, int(line)) for i, line in enumerate(fin))
numbers = deque(order)
answer = mix(order, numbers)
print('Part 1:', answer)
order = tuple((i, v * 811589153) for i, v in order)
numbers = deque(order)
answer = mix(order, numbers, 10)
print('Part 2:', answer)