From: Maximilian Friedersdorff Date: Wed, 7 Dec 2022 22:04:15 +0000 (+0000) Subject: Do day 7 X-Git-Url: https://git.friedersdorff.com/?a=commitdiff_plain;h=HEAD;p=max%2Faoc_2022.git Do day 7 --- diff --git a/7.py b/7.py new file mode 100644 index 0000000..26fba88 --- /dev/null +++ b/7.py @@ -0,0 +1,85 @@ +"""Pretend we're du""" +import pprint +import sys + + +""" +fs = { + "dir /": 0, + "/siten": 34987, + "/itee": 3478, + "/esntien": 38787, + "dir /a": 0, + "/aiensten": 378, + "/a/iensent": 1097, + "/a/b/c/d/e/g/fensteinsrteni": 347347, + "dir /a/b": 0, +} +""" + + +def populate_dir_sizes(fs): + for path, size in fs.items(): + if path.startswith("dir "): + parent = path.split(" ")[1] + if parent == "/": + parent = "" + + for child_path, child_size in fs.items(): + if child_path.startswith(f"{parent}/"): + fs[path] += child_size + + return fs + + +fs = {"dir /": 0} +current_path = [] + +with open("7.in") as fh: + for line in fh: + line = line.strip() + + if line.startswith("$ cd "): + if line[5:] == "/": + current_path = [""] + elif line[5:] == "..": + current_path.pop() + else: + current_path.append(line[5:]) + + continue + + if line.startswith("$ ls"): + continue + + size, name = line.split(" ") + if size == "dir": + fs[f"dir {'/'.join(current_path + [name])}"] = 0 + else: + fs["/".join(current_path + [name])] = int(size) + +fs = populate_dir_sizes(fs) + + +total_size = 0 +for path, size in fs.items(): + if path.startswith("dir ") and size <= 100000: + total_size += size + + +print(total_size) + + +fs_size = fs["dir /"] +disk_size = 70000000 +update_size = 30000000 +remaining_disk_space = disk_size - fs_size +extra_space_required = update_size - remaining_disk_space + +candidates_for_deletion = [] +for path, size in fs.items(): + if path.startswith("dir ") and size >= extra_space_required: + candidates_for_deletion.append((path, size)) + +candidates_for_deletion.sort(key=lambda x: x[1]) +print(candidates_for_deletion[0])