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.

53 lines
1.2 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 dirs.mb() <= 100000:
sizes.append(dirs.mb())
print(sum(sizes))