15 "/a/b/c/d/e/g/fensteinsrteni": 347347,
21 def populate_dir_sizes(fs):
22 for path, size in fs.items():
23 if path.startswith("dir "):
24 parent = path.split(" ")[1]
28 for child_path, child_size in fs.items():
29 if child_path.startswith(f"{parent}/"):
30 fs[path] += child_size
38 with open("7.in") as fh:
42 if line.startswith("$ cd "):
45 elif line[5:] == "..":
48 current_path.append(line[5:])
52 if line.startswith("$ ls"):
55 size, name = line.split(" ")
57 fs[f"dir {'/'.join(current_path + [name])}"] = 0
59 fs["/".join(current_path + [name])] = int(size)
61 fs = populate_dir_sizes(fs)
65 for path, size in fs.items():
66 if path.startswith("dir ") and size <= 100000:
75 update_size = 30000000
76 remaining_disk_space = disk_size - fs_size
77 extra_space_required = update_size - remaining_disk_space
79 candidates_for_deletion = []
80 for path, size in fs.items():
81 if path.startswith("dir ") and size >= extra_space_required:
82 candidates_for_deletion.append((path, size))
84 candidates_for_deletion.sort(key=lambda x: x[1])
85 print(candidates_for_deletion[0])