Skip to content

Instantly share code, notes, and snippets.

@0racle
Created December 5, 2022 18:36
Show Gist options
  • Save 0racle/7920306ab93cf3936bf047a788ec0998 to your computer and use it in GitHub Desktop.
Save 0racle/7920306ab93cf3936bf047a788ec0998 to your computer and use it in GitHub Desktop.
AoC 2022 Day 5
import re
cx, mx = map(str.splitlines, open('input').read().split("\n\n"))
batch = lambda xs, n: (xs[i : n + i] for i in range(0, len(xs), n))
c = [[x[1].strip() for x in batch(line, 4)] for line in cx[:-1]]
c = [list(filter(len, x)) for x in zip(*c)]
d = [e.copy() for e in c.copy()] # deepcopy for part 2
for line in mx:
m, f, t = map(int, re.findall(r'\d+', line))
b = list(reversed(c[f - 1][:m]))
del c[f - 1][:m]
c[t - 1] = b + c[t - 1]
print(''.join(e[0] for e in c))
for line in mx:
m, f, t = map(int, re.findall(r'\d+', line))
b = list(d[f - 1][:m])
del d[f - 1][:m]
d[t - 1] = b + d[t - 1]
print(''.join(e[0] for e in d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment