-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-part2.py
39 lines (35 loc) · 990 Bytes
/
05-part2.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
#!/usr/bin/python3
import re
import fileinput
def exec_move(command):
global crane
regex = re.compile('^move ([0-9]+) from ([0-9]+) to ([0-9]+)$')
result = regex.match(command)
if result:
quantity = int(result[1])
src = int(result[2]) - 1
dst = int(result[3]) - 1
tmp = crane[src][-quantity:]
crane[src] = crane[src][:-quantity]
crane[dst].extend(tmp)
else:
raise Exception("error")
loading = True
crane = [ [], [], [], [], [], [], [], [], [] ]
for line in fileinput.input():
line = line.rstrip('\n')
if line == "":
continue
if loading == True:
if line == " 1 2 3 4 5 6 7 8 9 ":
loading = False
continue
for i in range(1,34,4):
index = int((i - 1) / 4)
if line[i].rstrip() != "":
crane[index].insert(0,line[i])
else:
exec_move(line)
for i in crane:
print(i[-1],end='')
print('')