-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
49 lines (37 loc) · 1.22 KB
/
day7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import fileinput
filesystem = dict[str, int | "filesystem"]
root: filesystem = {}
stack: list[filesystem] = []
current = root
for line in fileinput.input():
match line.strip().split():
case ["$", "cd", "/"]:
stack = []
current = root
case ["$", "cd", ".."]:
current = stack.pop()
case ["$", "cd", dir]:
stack.append(current)
current = current[dir]
case ["$", "ls"]:
pass
case ["dir", dir]:
current[dir] = {}
case [size, filename]:
current[filename] = int(size)
case _:
raise Exception
MAX = 100_000
def get_size(object: int | filesystem, list_of_all_sizes: list[int]) -> int:
if type(object) == dict:
sum_thingy = sum(get_size(x, list_of_all_sizes) for x in object.values())
list_of_all_sizes.append(sum_thingy)
return sum_thingy
return object
list_of_all_sizes: list[int] = []
root_size = get_size(root, list_of_all_sizes)
print(sum(x for x in list_of_all_sizes if x <= MAX))
TOTAL_NEEDED = 30_000_000
DISK_SIZE = 70_000_000
still_needed = TOTAL_NEEDED - (DISK_SIZE - root_size)
print(min(x for x in list_of_all_sizes if x >= still_needed))