with open('input5.txt','r') as f: inp = f.read().splitlines(keepends=False) stacks = {1 : [], 2 : [], 3 : [], 4 : [], 5 : [], 6 : [], 7 : [], 8 : [], 9 : []} instructions = [ [], [], []] x = True y = 0 for i in inp: if x: if i == '': x = False else: y = 0 for z in range(1,10): tempa = i[y:y+3] if " " not in tempa and tempa != "": stacks[z].append(tempa) y += 4 else: tempa, amou, tempb, orig, tempc, dest = i.split(' ') instructions[0].append(int(amou)) instructions[1].append(int(orig)) instructions[2].append(int(dest)) for i in range(len(instructions[0])): tempa = [] for x in range(instructions[0][i]): tempa.append(stacks[instructions[1][i]][x]) del stacks[instructions[1][i]][0:instructions[0][i]] tempa.reverse() for y in tempa: stacks[instructions[2][i]].insert(0, y) for i in range(1,10): print(stacks[i][0])