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

28 lines
685 B
Python
Raw Normal View History

2022-12-05 09:17:50 +00:00
import fileinput
2022-12-05 09:39:58 +00:00
import sys
2022-12-05 09:17:50 +00:00
from collections import defaultdict, deque
import re
from copy import deepcopy
2022-12-05 09:39:58 +00:00
input = sys.stdin.read().split("\n")
2022-12-05 09:17:50 +00:00
2022-12-05 09:39:58 +00:00
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)]
2022-12-05 09:17:50 +00:00
stacks2 = deepcopy(stacks)
2022-12-05 09:39:58 +00:00
2022-12-05 09:17:50 +00:00
RE = re.compile(r"move (\d+) from (\d+) to (\d+)")
for line in input:
m = RE.match(line)
if not m:
continue
2022-12-05 09:39:58 +00:00
n, f, t = int(m[1]), int(m[2]) - 1, int(m[3]) - 1
2022-12-05 09:17:50 +00:00
2022-12-05 09:39:58 +00:00
stacks[t].extend(reversed(stacks[f][-n:]))
del stacks[f][-n:]
2022-12-05 09:17:50 +00:00
2022-12-05 09:39:58 +00:00
stacks2[t].extend(stacks2[f][-n:])
del stacks2[f][-n:]
2022-12-05 09:17:50 +00:00
print("".join(s[-1] for s in stacks), "".join(s[-1] for s in stacks2))