Roderic Day 2 lat temu
rodzic
commit
45bac1ea5d
2 zmienionych plików z 31 dodań i 1 usunięć
  1. +1
    -1
      makefile
  2. +30
    -0
      y2022/p07.py

+ 1
- 1
makefile Wyświetl plik

@@ -10,7 +10,7 @@ DAY := $(shell echo ${CODE} | sed 's/[^0-9]/ /g' | cut -d' ' -f6)
URL := https://adventofcode.com/${YEAR}/day/`echo ${DAY} | bc`/input

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

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

+ 30
- 0
y2022/p07.py Wyświetl plik

@@ -0,0 +1,30 @@
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)

Ładowanie…
Anuluj
Zapisz