aboutsummaryrefslogtreecommitdiff
path: root/2022/07/solve.py
diff options
context:
space:
mode:
authorMarvin Borner2022-12-07 06:42:24 +0100
committerMarvin Borner2022-12-07 06:42:24 +0100
commitd9a92b10741fab1ff61064107fd61610a8c16dbc (patch)
treed7faaa103b839e8eeb6c2d93ebc9ca2b881a933e /2022/07/solve.py
parent5945f4e31ea287d6b7bfaebe9c504e8807a4510c (diff)
Needs cleanup :D
Diffstat (limited to '2022/07/solve.py')
-rw-r--r--2022/07/solve.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/2022/07/solve.py b/2022/07/solve.py
new file mode 100644
index 0000000..4217750
--- /dev/null
+++ b/2022/07/solve.py
@@ -0,0 +1,54 @@
+data = [dat.strip() for dat in open("input").readlines() if dat != '']
+
+def sum_dir(d):
+ s = 0
+ for file in d:
+ s += int(file[1])
+ return s
+
+def solve():
+ fs = {"/": []}
+ path = []
+
+ i = 0
+ while i < len(data):
+ line = data[i]
+ i += 1
+ if line[0] == "$":
+ if line[2] == "c":
+ if line[5:] == "..":
+ del path[-1]
+ else:
+ path.append(line[5:])
+ if line[2] == "l":
+ top = fs["/".join(path)]
+ if not top:
+ top = []
+ while i < len(data) and data[i][0] != "$":
+ if data[i][0] == "d":
+ fs["/".join(path)+"/"+data[i][4:]] = []
+ else:
+ s,n = data[i].split(" ")
+ fs["/".join(path)].append((n, s))
+ i += 1
+
+ aah = []
+ for elem in fs:
+ s = sum_dir(fs[elem])
+ for sub in fs:
+ if sub != elem and sub.startswith(elem):
+ s += sum_dir(fs[sub])
+ aah.append(s)
+
+ used = aah[0]
+ total = 70000000-used
+ to_free = 30000000-total
+
+ to_delete = []
+ for elem in aah:
+ if elem > to_free:
+ to_delete.append(elem)
+
+ return min(to_delete)
+
+print(solve())