-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd5.py
45 lines (40 loc) · 1 KB
/
d5.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
stacklines = []
moves = []
phase = 1
part = 1
# for l in open("t5.txt"):
for l in open("i5_2.txt"):
if l.strip() == "":
phase = 2
elif l.strip()[0] == '1':
n = int(l.strip().split()[-1])
elif phase == 1:
stacklines.append(l.strip('\n'))
else:
moves.append(l.strip())
# print(stacklines)
# print(moves)
stacks = [ list() for x in range(n)]
for l in reversed(stacklines):
for k, i in enumerate(range(1, n*4+1, 4)):
x = l[i]
if x != ' ':
stacks[k].append(x)
# print(stacks)
for m in moves:
_, c, _, f, _, t = m.split()
# print(c,f,t)
popped = []
if part == 1:
for i in range(int(c)):
x = stacks[int(f)-1].pop()
stacks[int(t)-1].append(x)
else:
for i in range(int(c)):
x = stacks[int(f)-1].pop()
popped.append(x)
for i in range(int(c)):
x = popped.pop()
stacks[int(t)-1].append(x)
res = [x[-1] for x in stacks]
print(''.join(res))