From 32be41efc87deddb71190e8ca50578c8c3005ef8 Mon Sep 17 00:00:00 2001 From: kwout Date: Sat, 2 Dec 2023 14:37:29 -0500 Subject: [PATCH] 2022 day 7a solved --- 2022/7.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2022/7.py diff --git a/2022/7.py b/2022/7.py new file mode 100644 index 0000000..a3c7787 --- /dev/null +++ b/2022/7.py @@ -0,0 +1,29 @@ +lines = [[l.split(" ") for l in m.splitlines()] for m in open("input.txt", 'r').read().split("$ ")][1:] + +path = ["/"] +sizes = {"/":0} +sum = 0 + +for l in lines: + if l[0][0] == "cd": + if l[0][1] == "/": + path = ["/"] + elif l[0][1] == "..": + path.pop() + else: + path.append(l[0][1]+"/") + elif l[0][0] == "ls": + for i in range(1,len(l)): + if l[i][0] == "dir": + sizes["".join(path) + l[i][1]+"/"] = 0 + else: + sofar = "" + for p in path: + sofar += p + sizes[sofar] += int(l[i][0]) + +for s in sizes: + if sizes[s] <= 100000: + sum += sizes[s] + +print(sum) \ No newline at end of file