aboutsummaryrefslogtreecommitdiff
path: root/2022/07
diff options
context:
space:
mode:
authorMarvin Borner2022-12-07 07:04:38 +0100
committerMarvin Borner2022-12-07 07:04:38 +0100
commit1da4f10f68255fe5ddae10b30106c42372c261a0 (patch)
tree325f7fd4604a6b4a2fc0fd6a6db85d09d49592fb /2022/07
parentd9a92b10741fab1ff61064107fd61610a8c16dbc (diff)
Maybe a bit better
Diffstat (limited to '2022/07')
-rw-r--r--2022/07/solve.py63
1 files changed, 25 insertions, 38 deletions
diff --git a/2022/07/solve.py b/2022/07/solve.py
index 4217750..95ae42d 100644
--- a/2022/07/solve.py
+++ b/2022/07/solve.py
@@ -1,11 +1,5 @@
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 = []
@@ -14,41 +8,34 @@ def solve():
while i < len(data):
line = data[i]
i += 1
- if line[0] == "$":
- if line[2] == "c":
- if line[5:] == "..":
- del path[-1]
+ if line[2] == "c":
+ if line[5:] == "..":
+ del path[-1]
+ else:
+ path.append(line[5:])
+ if line[2] == "l":
+ top = fs["/".join(path)]
+ while i < len(data) and data[i][0] != "$":
+ if data[i][0] == "d":
+ fs["/".join(path)+"/"+data[i][4:]] = []
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
+ s,_ = data[i].split(" ")
+ fs["/".join(path)].append(int(s))
+ i += 1
- aah = []
+ all_sizes = []
+ part1 = 0
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
+ s = sum(fs[elem])
+ s += sum(sum(fs[sub]) for sub in fs if sub != elem and sub.startswith(elem))
+ if s < 100000:
+ part1 += s
+ all_sizes.append(s)
- to_delete = []
- for elem in aah:
- if elem > to_free:
- to_delete.append(elem)
+ print(f"Part 1: {part1}")
- return min(to_delete)
+ to_free = 30000000-70000000+all_sizes[0]
+ part2 = min(elem for elem in all_sizes if elem > to_free)
+ print(f"Part 2: {part2}")
-print(solve())
+solve()