data = open("input.txt", 'r').read() stacks, instructions = data.split("\n\n") stacks = stacks.splitlines() instructions = [i.split(" ") for i in instructions.splitlines()] s = [] for i in range(int(stacks[-1][-2])): s.append([]) for j in range(len(stacks)-1): crate = stacks[-j-2][i*4+1] if crate != " ": s[i].append(crate) for i in instructions: for j in range(int(i[1])): s[int(i[5])-1].append(s[int(i[3])-1].pop()) print("".join([t.pop() for t in s]))