-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
85 lines (65 loc) · 2.54 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
from collections import defaultdict
class Day07:
def __init__(self):
self.puzzle_input = self.load_input()
def part_one(self) -> int:
bags = self.get_bags_sorted(self.puzzle_input)
counted_bags = set()
for key in bags.keys():
if self.contains_shiny_gold(bags, key, counted_bags):
counted_bags.add(key)
return len(counted_bags)
def part_two(self) -> int:
bags = self.get_bags_sorted(self.puzzle_input)
return self.get_bags_inside("shiny gold", bags)
@staticmethod
def get_bags_sorted(puzzle_input: list) -> defaultdict:
bags = defaultdict(list)
for line in puzzle_input:
splitted = line.split()
current_bag_name = splitted[0] + " " + splitted[1]
# Contains at least one bag
if splitted[4].isdigit():
additional_bags = []
sum_bags = len(splitted[4:]) // 4
name_index, amount_index = 5, 4
for _ in range(1, sum_bags + 1):
child_bag_name = (
splitted[name_index] + " " + splitted[name_index + 1]
)
additional_bags.append(
{"name": child_bag_name, "amount": int(splitted[amount_index])}
)
name_index += 4
amount_index += 4
bags[current_bag_name].extend(additional_bags)
return bags
def get_bags_inside(self, key, bags) -> int:
bag_contents = bags.get(key)
if bag_contents is None:
return 0
total_bags = 0
for val in bag_contents:
total_bags += val["amount"] + val["amount"] * self.get_bags_inside(
val["name"], bags
)
return total_bags
def contains_shiny_gold(self, bags, key, counted_bags) -> bool:
if bags.get(key) is None:
return False
if any(entry.get("name") == "shiny gold" for entry in bags.get(key)):
return True
for val in bags.get(key):
if self.contains_shiny_gold(bags, val["name"], counted_bags):
counted_bags.add(val["name"])
return True
@staticmethod
def load_input() -> list:
list_input = []
with open("input.txt") as file:
for line in file:
list_input.append(line.rstrip())
return list_input
day = Day07()
print(f"Result part 1: {day.part_one()}")
print(f"Result part 2: {day.part_two()}")