您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

21 行
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))