浏览代码

🕺

undefined
Roderic Day 2 年前
父节点
当前提交
9cc4faf313
共有 2 个文件被更改,包括 31 次插入1 次删除
  1. +1
    -1
      makefile
  2. +30
    -0
      y2022/p07.py

+ 1
- 1
makefile 查看文件

URL := https://adventofcode.com/${YEAR}/day/`echo ${DAY} | bc`/input URL := https://adventofcode.com/${YEAR}/day/`echo ${DAY} | bc`/input


pyrun: ${DATA} pyrun: ${DATA}
PYTHONPATH=. python3 -u ${CODE} < ${DATA}
cat ${DATA} | docker run -v `pwd`:/app/ -w /app/ -i --rm python:latest python -u ${CODE}


${DATA}: ${DATA}:
# avoid spam in the lead up to the event # avoid spam in the lead up to the event

+ 30
- 0
y2022/p07.py 查看文件

import collections
import itertools


text = open(0).read()


path = []
dirs = collections.defaultdict(int)
for line in map(str.split, text.splitlines()):
match line:
case ('$', 'cd', '..'):
path.pop()
case ('$', 'cd', loc):
path.append(loc)
case ('$', 'ls'):
pass
case ('dir', _):
pass
case (size, _):
for leg in itertools.accumulate(path):
dirs[leg] += int(size)


ans1 = sum(size for size in dirs.values() if size <= 100_000)
print(ans1)

available = 70_000_000 - dirs['/']
ans2 = min(filter(lambda size: available + size >= 30_000_000, dirs.values()))
print(ans2)

正在加载...
取消
保存