瀏覽代碼

🍵

master
Roderic Day 2 年之前
父節點
當前提交
c2477ec957
共有 2 個檔案被更改,包括 32 行新增0 行删除
  1. +12
    -0
      y2022/p04.py
  2. +20
    -0
      y2022/p05.py

+ 12
- 0
y2022/p04.py 查看文件

@@ -0,0 +1,12 @@
import re


ans1 = ans2 = 0
for line in open(0).read().splitlines():
a, b, c, d = map(int, re.findall(r'\d+', line))
ab = set(range(a, b + 1))
cd = set(range(c, d + 1))
ans1 += ab <= cd or cd <= ab
ans2 += any(ab & cd)
print(ans1)
print(ans2)

+ 20
- 0
y2022/p05.py 查看文件

@@ -0,0 +1,20 @@
import re
import copy


top, bottom = open(0).read().split('\n\n')
transpose = '\n'.join(map(''.join, zip(*top.splitlines())))
stacks = [list(ln[::-1][1:].strip()) for ln in transpose.splitlines()[1::4]]


def operate(stacks, is_upgraded):
for line in bottom.splitlines():
qty, fro, to = map(int, re.findall(r'\d+', line))
fro, to = fro - 1, to - 1 # shift
carry = [stacks[fro].pop() for _ in range(qty) if stacks[fro]]
stacks[to].extend(carry[::-1] if is_upgraded else carry)
return ''.join(s[-1] for s in stacks if s)


print(operate(copy.deepcopy(stacks), 0))
print(operate(copy.deepcopy(stacks), 1))

Loading…
取消
儲存