58 lines
1.4 KiB
Python
58 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import re
|
|
import copy
|
|
|
|
test = (""" [D]
|
|
[N] [C]
|
|
[Z] [M] [P]
|
|
1 2 3
|
|
|
|
move 1 from 2 to 1
|
|
move 3 from 1 to 3
|
|
move 2 from 2 to 1
|
|
move 1 from 1 to 2""".splitlines())
|
|
|
|
|
|
def build_stacks(input):
|
|
number = (len(input[0]) // 4) + 1
|
|
stacks = [[] for i in range(number)]
|
|
for line in input:
|
|
if line[1] == "1":
|
|
break
|
|
for crate in range(0, number):
|
|
if( not line[(1 + (crate * 4))] == " " ):
|
|
stacks[crate].insert(0, line[(1 + (crate * 4))])
|
|
return stacks
|
|
|
|
def rearrange_stacks( input ):
|
|
stack = build_stacks( input )
|
|
for step in input:
|
|
if len(step) == 0 or not step[0] == "m":
|
|
next
|
|
else:
|
|
number, s1, s2 = re.findall(r"\b(\d+)", step)
|
|
# print("%s %s %s" % (number, s1, s2))
|
|
crates = stack[int(s1) - 1][-abs(int(number)):]
|
|
stack[int(s1) - 1] = stack[int(s1) - 1][0:-abs(int(number))]
|
|
stack[int(s2) -1] = stack[int(s2) -1] + crates
|
|
return stack
|
|
|
|
def read_stacks_top( stacks ):
|
|
stacks_c = copy.deepcopy(stacks)
|
|
top = list(map(list.pop, stacks_c))
|
|
print("".join(top))
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
file = open("input","r")
|
|
lines = file.read().splitlines()
|
|
# print(test)
|
|
# print(build_stacks(test)
|
|
# stacks_r = (rearrange_stacks(test))
|
|
# read_stacks_top(stacks_r)
|
|
stacks_r = rearrange_stacks(lines)
|
|
read_stacks_top(stacks_r)
|