black formatter

This commit is contained in:
2022-12-08 14:18:03 +01:00
parent 701c168ad1
commit cb69657218

View File

@@ -1,24 +1,24 @@
from collections import defaultdict
from aocd.models import Puzzle from aocd.models import Puzzle
from aocd import submit from aocd import submit
def get_sizes_a(dirs: dict, max_size: int) -> int: def get_sizes_a(dirs: dict, max_size: int) -> int:
""" """
return the size of all dirs which are at most max_size big return the size of all dirs which are at most max_size big
""" """
filtered_dirs = [ value for value in dirs.values() if value < max_size] filtered_dirs = [value for value in dirs.values() if value < max_size]
return sum(filtered_dirs) return sum(filtered_dirs)
def get_sizes_b(dirs: dict, needed_size: int) -> int: def get_sizes_b(dirs: dict, needed_size: int) -> int:
""" """
return the smallest size of a dir which when deleted amounts return the smallest size of a dir which when deleted amounts
to the needed size of 30M. to the needed size of 30M.
""" """
total_size = 70000000 total_size = 70000000
used_size = dirs[('/',)] used_size = dirs[("/",)]
max_free = needed_size - (total_size - used_size) max_free = needed_size - (total_size - used_size)
filtered_dirs = [ value for value in dirs.values() if value > max_free] filtered_dirs = [value for value in dirs.values() if value > max_free]
return min(filtered_dirs) return min(filtered_dirs)
@@ -34,25 +34,27 @@ def parse_input(input_str: str) -> list:
available information in the following dictionary. available information in the following dictionary.
- {list['/','dir','path']:int(size_sum)} - {list['/','dir','path']:int(size_sum)}
""" """
path = [] path = list()
dirs = defaultdict(int) dirs = dict()
for line in input_str.splitlines(): for line in input_str.splitlines():
match line.split(): match line.split():
case ('$', 'cd', '..'): case ("$", "cd", ".."):
path.pop() path.pop()
case ('$', 'cd', name): case ("$", "cd", name):
path.append(name) path.append(name)
case ('$', 'ls'): case ("$", "ls"):
continue continue
case ('dir', _): case ("dir", _):
continue continue
case (size, file): case (size, file):
size = int(size) size = int(size)
for i in range(len(path)): for i in range(len(path)):
key = tuple(path[:i+1]) # list not hashable key = tuple(path[: i + 1]) # list not hashable
dirs[key] += size dirs.setdefault(key, 0) # initialize key for += to work
dirs[key] += size # dirs needs to be initialized
return dirs return dirs
if __name__ == "__main__": if __name__ == "__main__":
# get puzzle and parse data # get puzzle and parse data
puzzle = Puzzle(year=2022, day=7) puzzle = Puzzle(year=2022, day=7)