Skip to content

Instantly share code, notes, and snippets.

@andreypopp
Last active December 7, 2022 20:47
Show Gist options
  • Save andreypopp/a46ff866f4d9efed9737535704e6054b to your computer and use it in GitHub Desktop.
Save andreypopp/a46ff866f4d9efed9737535704e6054b to your computer and use it in GitHub Desktop.
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