from string import ascii_letters DEBUG=False STACKS_START = """FCPGQR WTCP BHPMC LTQSMPR PHJZVGN DPJ LGPZFJTR NLHCFPTJ GVZQHTCW""" def debug(*args): if DEBUG: print(args) def main(): stacks = {} stacks_lines = STACKS_START.split("\n") for i, line in enumerate(stacks_lines): j = i+1 for l in line: p = stacks.get(j, []) p.append(l) stacks[j] = p print(stacks) with open("input5.txt", "r") as file: inlines = [line.strip() for line in file.readlines()] instructions = [] for line in inlines: words = line.split(" ") moves = [] for word in words: try: moves.append(int(word)) except ValueError: pass instructions.append(moves) for move in instructions: number, stack_from, stack_to = move n_num = 0-number f = stacks[stack_from] t = stacks[stack_to] f_moved = f[n_num:] f_left = f[:n_num] t.extend(f_moved) stacks[stack_from] = f_left stacks[stack_to] = t for num, stack in stacks.items(): print(f"{num}: {stack[-1]}") if __name__ == "__main__": main()