Last active
December 7, 2022 20:47
-
-
Save andreypopp/a46ff866f4d9efed9737535704e6054b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
path = [] | |
dirs = set() | |
size = {} | |
lines = [line.strip() for line in open("./aoc7.data")] | |
while lines: | |
line = lines.pop(0) | |
if line == "$ cd /": line = "$ cd C:" | |
if line == "$ cd ..": | |
path.pop() | |
elif line.startswith("$ cd "): | |
name = line[5:] | |
path.append(name) | |
dirs.add("/".join(path)) | |
elif line == "$ ls": | |
while lines and not lines[0].startswith("$ "): | |
line = lines.pop(0) | |
if line.startswith("dir "): | |
continue | |
else: | |
s,n = line.split(" ") | |
k = "/".join(path + [n]) | |
assert k not in size | |
size[k] = int(s) | |
else: | |
assert false | |
def subdirsize(p): | |
return sum(v for k, v in size.items() if k.startswith(p+"/")) | |
totsize = {} | |
for d in dirs: | |
totsize[d] = subdirsize(d) | |
totsize1 = {k:v for k, v in totsize.items() if v < 100_000} | |
print(dirs) | |
print(size) | |
print(totsize) | |
print(totsize1) | |
print(sum(v for v in totsize1.values())) | |
total = 70000000 | |
enough = 30000000 | |
used = totsize['C:'] | |
print({k: v for k, v in totsize.items() if total - used + v >= enough}) | |
print(min(v for k, v in totsize.items() if total - used + v >= enough)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment