You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 line
645B

  1. import re
  2. import copy
  3. top, bottom = open(0).read().split('\n\n')
  4. transpose = '\n'.join(map(''.join, zip(*top.splitlines())))
  5. stacks = [list(ln[::-1][1:].strip()) for ln in transpose.splitlines()[1::4]]
  6. def operate(stacks, is_upgraded):
  7. for line in bottom.splitlines():
  8. qty, fro, to = map(int, re.findall(r'\d+', line))
  9. fro, to = fro - 1, to - 1 # shift
  10. carry = [stacks[fro].pop() for _ in range(qty) if stacks[fro]]
  11. stacks[to].extend(carry[::-1] if is_upgraded else carry)
  12. return ''.join(s[-1] for s in stacks if s)
  13. print(operate(copy.deepcopy(stacks), 0))
  14. print(operate(copy.deepcopy(stacks), 1))