Skip to content

Commit ba1ead8

Browse files
committed
Add function that size of the smallest directory that should be deleted
1 parent f15e860 commit ba1ead8

File tree

1 file changed

+11
-1
lines changed
  • puzzles/solutions/2022/d07

1 file changed

+11
-1
lines changed

puzzles/solutions/2022/d07/p2.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import sys
22

3+
import consts
4+
import p1
5+
36

47
def get_answer(input_text: str):
5-
raise NotImplementedError
8+
"""Return total size of the smallest directory that, if deleted, would free up enough space on the filesystem."""
9+
filesystem = p1.get_filesystem(input_text)
10+
root = filesystem[0]
11+
total_used_space = root.size
12+
unused_space_needed = consts.REQUIRED_UNUSED_SPACE -(consts.TOTAL_DISK_SPACE - total_used_space)
13+
candidate_directories = (directory for directory in filesystem if directory.size >= unused_space_needed)
14+
directory_to_delete = min(candidate_directories, key=lambda directory: directory.size)
15+
return directory_to_delete.size
616

717

818
if __name__ == "__main__":

0 commit comments

Comments
 (0)