import sys with open("input") as f: data = f.read() a, b = data.split("\n\n") stacks = [ "FCJPHTW", "GRVFZJBH", "HPTR", "ZSNPHT", "NVFZHJCD", "PMGFWDZ", "MVZWSJDP", "NDS", "DZSFM", ] ins = b.splitlines() stacks1 = [list(x) for x in stacks] stacks2 = [list(x) for x in stacks] for i in ins: #print(i) _, n, _, f, _, t = i.split() n = int(n) f = int(f) t = int(t) # part 1 for _ in range(n): #print(stacks1[t-1]) #print(stacks1[f-1]) stacks1[t-1].append(stacks1[f-1].pop()) # part 2 stacks2[t-1].extend(stacks2[f-1][-n:]) stacks2[f-1] = stacks2[f-1][:-n] print("".join(x[-1] for x in stacks1)) print("".join(x[-1] for x in stacks2))