-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.py
62 lines (41 loc) · 994 Bytes
/
15.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
#!/usr/bin/env python2.7
import sys
import re
from pprint import pprint
#from operator import itemgetter
l = re.compile(r'Disc #(\d) has (\d+) positions; at time=0, it is at position (\d+).')
numpos = []
startpos = []
while True:
try:
line = sys.stdin.readline().rstrip()
m = l.match(line)
if m:
numpos.append(int(m.group(2)))
startpos.append(int(m.group(3)))
except KeyboardInterrupt:
break
if not line:
break
print(numpos)
print(startpos)
print('------')
t = 0
disc = 0
num_discs = len(numpos)
def posat(t):
return [(s+i+t)%n for (i,s,n) in zip(range(len(numpos)), startpos, numpos)]
def find_miss(p):
for (d,x) in enumerate(p):
if x:
return d
return None
m = 0
while True:
positions = posat(t+1)
m = find_miss(positions)
if m == None:
break
time_needed = ( - positions[m]) % numpos[m]
t+=time_needed
print(t, positions, time_needed)