aoc
/
2022
1
0
Fork 0

day 5: god save me

This commit is contained in:
Sijmen 2022-12-05 10:17:50 +01:00
parent a6b5ccc5f3
commit 101df22f4a
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
1 changed files with 42 additions and 0 deletions

42
day05.py Normal file
View File

@ -0,0 +1,42 @@
import fileinput
from collections import defaultdict, deque
import re
from copy import deepcopy
input = fileinput.input()
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])
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]), int(m[3])
p2stack = []
for _ in range(n):
c = stacks[f - 1].pop()
stacks[t - 1].append(c)
c = stacks2[f - 1].pop()
p2stack.append(c)
stacks2[t - 1].extend(reversed(p2stack))
print("".join(s[-1] for s in stacks), "".join(s[-1] for s in stacks2))