You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

lines = open("input.txt", "r").read().splitlines()
class Directory:
def __init__(self, path, parent):
self.path = path
self.parent = parent
self.children = []
self.files = []
def mb(self):
val_self = sum(self.files)
for child in self.children:
val_self = val_self + paths[child].mb()
return val_self
ls = False
root = Directory("/", None)
cwd = root
paths = {"/": root}
for line in lines:
cmd = line.split(" ")
if cmd[0] == "$":
ls = False
if cmd[1] == "cd":
if cmd[2] == "..":
path = cwd.parent.path
cwd = cwd.parent
elif cmd[2] == "/":
path = "/"
cwd = root
else:
path = cwd.path + cmd[2] + "/"
if path not in paths:
paths[path] = Directory(path, cwd)
cwd = paths[path]
if cmd[1] == "ls":
ls = True
elif ls:
if cmd[0] != "dir":
cwd.files.append(int(cmd[0]))
else:
cwd.children.append(cwd.path + cmd[1] + "/")
sizes = {}
for dirs in paths.values():
if (70000000 - paths["/"].mb()) + dirs.mb() >= 30000000:
sizes[dirs.path] = dirs.mb()
smallest = 70000000
for i in sizes:
if sizes[i] < smallest:
smallest = sizes[i]
print(smallest)