Move Python solutions

This commit is contained in:
Felix Bargfeldt 2023-10-19 22:06:58 +02:00
parent 6fae773051
commit 2f1872bbd9
Signed by: Defelo
GPG key ID: 2A05272471204DD3
255 changed files with 0 additions and 4323 deletions

53
Python/2022/07.py Normal file
View file

@ -0,0 +1,53 @@
import re
def get_input(puzzle: str) -> list[str]:
return puzzle.splitlines()
def get_dir_sizes(lines: list[str]) -> dict[tuple[str, ...], int]:
out = {}
seen = set()
pwd = ()
for line in lines:
if match := re.match(r"^\$ cd (.+)$", line):
d = match[1]
if d == "/":
pwd = ()
elif d == "..":
pwd = pwd[:-1]
else:
pwd = (*pwd, d)
elif match := re.match(r"^(\d+) (.+)$", line):
s = int(match[1])
n = match[2]
if (pwd, n) in seen:
continue
seen.add((pwd, n))
d = pwd
while True:
out[d] = out.get(d, 0) + s
if not d:
break
d = d[:-1]
return out
def part1(puzzle: str):
sizes = get_dir_sizes(get_input(puzzle))
return sum(s for s in sizes.values() if s <= 100000)
def part2(puzzle: str):
sizes = get_dir_sizes(get_input(puzzle))
free = 70000000 - sizes[()]
return min(s for s in sizes.values() if s >= 30000000 - free)
if __name__ == "__main__":
from aoc import run
run(2022, 7, part1, part2)