Browse Source

🕺

master
Roderic Day 2 years ago
parent
commit
45bac1ea5d
2 changed files with 31 additions and 1 deletions
  1. +1
    -1
      makefile
  2. +30
    -0
      y2022/p07.py

+ 1
- 1
makefile View File

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 View File

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)

free = 70_000_000 - dirs['/']
ans2 = min(size for size in dirs.values() if free + size >= 30_000_000)
print(ans2)

Loading…
Cancel
Save