-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_two.py
73 lines (52 loc) · 1.83 KB
/
part_two.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
from typing import Any, override
from infrastructure.solutions.base import Solution
class Year2015Day12Part2Solution(Solution):
FORBIDDEN_ITEMS = ('red',)
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, str]:
return {'items': json.loads(text_input)}
@classmethod
@override
def solve(cls, items: list[Any]) -> int:
"""
Time: O(n*m)
Space: O(n*m)
Where n - total number of objects,
m - maximum size of object
"""
items = cls.filter_forbidden(items)
return cls.sum_numbers(items)
@classmethod
def sum_numbers(cls, item: Any) -> int:
if isinstance(item, dict):
return sum(cls.sum_numbers(value) for value in item.values())
if isinstance(item, list):
return sum(cls.sum_numbers(value) for value in item)
if isinstance(item, int):
return item
return 0
@classmethod
def filter_forbidden(cls, input_obj):
if isinstance(input_obj, dict):
return cls.filter_forbidden_from_dict(input_obj)
if isinstance(input_obj, list):
return cls.filter_forbidden_from_list(input_obj)
return input_obj
@classmethod
def filter_forbidden_from_dict(cls, input_dict: dict) -> dict:
output_dict = {}
for key, val in input_dict.items():
if val in cls.FORBIDDEN_ITEMS:
return {}
output_dict[key] = cls.filter_forbidden(val)
return output_dict
@classmethod
def filter_forbidden_from_list(cls, input_list: list) -> list:
output_list = []
for val in input_list:
output_list.append(cls.filter_forbidden(val))
return output_list
if __name__ == '__main__':
print(Year2015Day12Part2Solution.main())