-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.py
More file actions
35 lines (27 loc) · 790 Bytes
/
5.py
File metadata and controls
35 lines (27 loc) · 790 Bytes
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
# python 5.py input5.txt
import sys
import re
file = open(sys.argv[1])
num_stacks = len(file.readline()) // 4
stacks = [[] for _ in range(num_stacks)]
file.seek(0)
for line in file:
if not "[" in line:
file.readline()
break
for i in range(num_stacks):
crate = line[i * 4 + 1]
if crate != " ":
stacks[i].append(line[i * 4 + 1])
for stack in stacks:
stack.reverse()
for line in file:
m = re.match("move (?P<num>\d+) from (?P<origin>\d+) to (?P<dest>\d+)", line)
num = int(m.group("num"))
origin = int(m.group("origin")) - 1
dest = int(m.group("dest")) - 1
for _ in range(num):
stacks[dest].append(stacks[origin].pop())
file.close()
solution = ("").join(stack[-1] for stack in stacks)
print(solution)