-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.py
40 lines (33 loc) · 1.17 KB
/
day07.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
from collections import defaultdict
from pathlib import Path
from src.common.utils import SolverFunctions
title = 'Day 7: No Space Left On Device'
parser_method = 'str_split'
handle_data = 'lines'
class SolveTheDay(SolverFunctions):
@staticmethod
def helper(_data):
curr_dir = Path('/')
dirs = defaultdict(int)
for line in _data:
match line:
case ['$', 'cd', new_dir]:
curr_dir = curr_dir / new_dir
curr_dir = curr_dir.resolve()
case [size, _] if size.isdigit():
size = int(size)
for path in [curr_dir, *curr_dir.parents]:
dirs[path] += size
return dirs
@classmethod
def level_1(cls, data):
maximum_size = 100000
return sum(size for size in cls.helper(data).values() if size <= maximum_size)
@classmethod
def level_2(cls, data):
available_space = 70000000
required_space = 30000000
dirs = cls.helper(data)
return min(
size for size in dirs.values() if dirs[Path('/')] - size <= available_space - required_space
)