adventofcode2022/day07/sol.py

39 lines
946 B
Python

import sys
from collections import defaultdict
cmds = open("input").read().split("$")
cwd = []
sizes = defaultdict(lambda: 0)
for cmd in cmds:
cmd = cmd.strip()
if not cmd:
continue
print(cmd)
if cmd.startswith("cd "):
dir = cmd.split()[1]
if dir == "..":
cwd.pop()
elif dir == "/":
cwd = []
else:
cwd.append(dir)
elif cmd.startswith("ls\n"):
files = cmd.splitlines()[1:]
for file in files:
sz, name = file.split()
if sz == 'dir':
pass
else:
print(cwd, name, sizes)
for i in range(len(cwd)+1):
sizes['/'.join(cwd[:i])] += int(sz)
else:
print("bad command:" +cmd)
print(sizes)
print(sum(sz for sz in sizes.values() if sz <= 100000))
print(min(sz for sz in sizes.values() if 70000000 - sizes[''] + sz >= 30000000))