aoc
/
2022
1
0
Fork 0
2022/day05.py

28 lines
685 B
Python

import fileinput
import sys
from collections import defaultdict, deque
import re
from copy import deepcopy
input = sys.stdin.read().split("\n")
rows = (line[1::4] for line in input[::-1] if "[" in line)
stacks = [[c for c in x if c != ' '] for x in zip(*rows)]
stacks2 = deepcopy(stacks)
RE = re.compile(r"move (\d+) from (\d+) to (\d+)")
for line in input:
m = RE.match(line)
if not m:
continue
n, f, t = int(m[1]), int(m[2]) - 1, int(m[3]) - 1
stacks[t].extend(reversed(stacks[f][-n:]))
del stacks[f][-n:]
stacks2[t].extend(stacks2[f][-n:])
del stacks2[f][-n:]
print("".join(s[-1] for s in stacks), "".join(s[-1] for s in stacks2))