kind of solve part a
This commit is contained in:
@@ -1,36 +1,58 @@
|
|||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from aocd.models import Puzzle
|
from aocd.models import Puzzle
|
||||||
from aocd import submit
|
from aocd import submit
|
||||||
|
|
||||||
|
|
||||||
def get_size(commands: list, dirs: list, size: int) -> int:
|
def get_sizes(dirs: dict, max_size: int) -> int:
|
||||||
"""
|
"""
|
||||||
parse tree structure and get size of all files > size
|
gather all filesizes from the input
|
||||||
"""
|
"""
|
||||||
print(commands)
|
filtered_dirs = [ value for value in dirs.values() if value < max_size]
|
||||||
print(dirs)
|
return sum(filtered_dirs)
|
||||||
print(size)
|
|
||||||
|
|
||||||
return 0
|
def parse_input(input_str: str) -> list:
|
||||||
|
|
||||||
|
|
||||||
def parse_input(data: str) -> (list,list):
|
|
||||||
"""
|
"""
|
||||||
parse input data
|
the input string consists of the following four message types
|
||||||
|
- $ cd .. -> pop dir name from stack
|
||||||
|
- $ cd [dir] -> push dir name to stack
|
||||||
|
- $ ls -> ignore
|
||||||
|
- dir [dir] -> ignore
|
||||||
|
- [size] [file] -> sum up the sizes of all files in this dir
|
||||||
|
parsing is done by implementing a state machine which gathers all
|
||||||
|
available information in the following dictionary.
|
||||||
|
- {list['/','dir','path']:int(size_sum)}
|
||||||
"""
|
"""
|
||||||
return ([],[])
|
path = []
|
||||||
|
dirs = defaultdict(int)
|
||||||
|
for line in input_str.splitlines():
|
||||||
|
match line.split():
|
||||||
|
case ('$', 'cd', '..'):
|
||||||
|
path.pop()
|
||||||
|
case ('$', 'cd', name):
|
||||||
|
path.append(name)
|
||||||
|
case ('$', 'ls'):
|
||||||
|
continue
|
||||||
|
case ('dir', _):
|
||||||
|
continue
|
||||||
|
case (size, file):
|
||||||
|
size = int(size)
|
||||||
|
for i in range(len(path)):
|
||||||
|
key = tuple(path[:i+1]) # list not hashable
|
||||||
|
dirs[key] += size
|
||||||
|
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)
|
||||||
commands, dirs = parse_input(puzzle.input_data)
|
dirs = parse_input(puzzle.input_data)
|
||||||
|
|
||||||
# part a:
|
# part a: get the sum of all dirs where the size is at most 100k
|
||||||
answer_a = get_size(commands, dirs, 1000)
|
answer_a = get_sizes(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:
|
||||||
answer_b = get_size(commands, dirs, 10000)
|
answer_b = get_sizes(dirs, 100)
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user