my brain hurts

This commit is contained in:
aaron
2022-12-08 00:32:11 +01:00
parent 1ea592b42e
commit 701c168ad1

View File

@@ -3,14 +3,25 @@ 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(dirs: dict, max_size: int) -> int:
""" """
gather all filesizes from the input 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:
"""
return the smallest size of a dir which when deleted amounts
to the needed size of 30M.
"""
total_size = 70000000
used_size = dirs[('/',)]
max_free = needed_size - (total_size - used_size)
filtered_dirs = [ value for value in dirs.values() if value > max_free]
return min(filtered_dirs)
def parse_input(input_str: str) -> list: def parse_input(input_str: str) -> list:
""" """
the input string consists of the following four message types the input string consists of the following four message types
@@ -48,11 +59,11 @@ if __name__ == "__main__":
dirs = parse_input(puzzle.input_data) dirs = parse_input(puzzle.input_data)
# part a: get the sum of all dirs where the size is at most 100k # part a: get the sum of all dirs where the size is at most 100k
answer_a = get_sizes(dirs, 100000) answer_a = get_sizes_a(dirs, 100000)
print(f"{answer_a}") print(f"{answer_a}")
submit(answer_a, part="a", day=7, year=2022) submit(answer_a, part="a", day=7, year=2022)
# part b: # part b: find the smallest D (huehue) that will free up at least 30M
answer_b = get_sizes(dirs, 100) answer_b = get_sizes_b(dirs, 30000000)
print(f"{answer_b}") print(f"{answer_b}")
#submit(answer_b, part="b", day=7, year=2022) submit(answer_b, part="b", day=7, year=2022)