aoc
/
2022
1
0
Fork 0

day 5, but mildly less cursed

This commit is contained in:
Sijmen 2022-12-05 10:39:58 +01:00
parent 101df22f4a
commit 612b61a1ac
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
1 changed files with 10 additions and 25 deletions

View File

@ -1,24 +1,13 @@
import fileinput import fileinput
import sys
from collections import defaultdict, deque from collections import defaultdict, deque
import re import re
from copy import deepcopy from copy import deepcopy
input = fileinput.input() input = sys.stdin.read().split("\n")
stacks: list[deque] = []
for y, line in enumerate(input):
line = line.rstrip("\n")
if "1" in line:
continue
if not line:
break
for x in range(0, len(line), 4):
if y == 0:
stacks.append(deque([line[x + 1]] if line[x + 1] != " " else []))
elif line[x + 1] != " ":
stacks[x // 4].appendleft(line[x + 1])
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) stacks2 = deepcopy(stacks)
RE = re.compile(r"move (\d+) from (\d+) to (\d+)") RE = re.compile(r"move (\d+) from (\d+) to (\d+)")
@ -27,16 +16,12 @@ for line in input:
if not m: if not m:
continue continue
n, f, t = int(m[1]), int(m[2]), int(m[3]) n, f, t = int(m[1]), int(m[2]) - 1, int(m[3]) - 1
p2stack = [] stacks[t].extend(reversed(stacks[f][-n:]))
for _ in range(n): del stacks[f][-n:]
c = stacks[f - 1].pop()
stacks[t - 1].append(c)
c = stacks2[f - 1].pop() stacks2[t].extend(stacks2[f][-n:])
p2stack.append(c) del stacks2[f][-n:]
stacks2[t - 1].extend(reversed(p2stack))
print("".join(s[-1] for s in stacks), "".join(s[-1] for s in stacks2)) print("".join(s[-1] for s in stacks), "".join(s[-1] for s in stacks2))