-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
executable file
·70 lines (49 loc) · 1.68 KB
/
day7.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
import re
rules = {}
with open('test.txt', 'r') as f:
ridiculous_rules = f.readlines()
def can_this_bag_hold_shiny_stuff(bag: str) -> bool:
if rules[bag] is None:
return False
if 'shiny gold' in rules[bag].keys():
return True
else:
for sub in rules[bag].keys():
if can_this_bag_hold_shiny_stuff(sub):
return True
return False
def how_many_bags_can_one_bag_carries(rule: str):
global rules
bag, inside = rule.split('bags contain')
if 'no other bags' in inside:
rules[bag.strip()] = None
else:
content = {}
for sub_bag in inside.split(','):
quantity = re.match(r'^([0-9]+) (\w+ \w+).+$', sub_bag.strip()).group(1).strip()
bag_type = re.match(r'^([0-9]+) (\w+ \w+).+$', sub_bag.strip()).group(2).strip()
if bag_type in content:
raise ValueError
content[bag_type] = int(quantity)
rules[bag] = content
def how_many_bags_can_fit_within_this_bag(root: dict, multiplier: int) -> int:
if root == None:
return 0
total = sum(root.values()) * multiplier
for bag, count in root.items():
total += how_many_bags_can_fit_within_this_bag(rules[bag], count*multiplier)
return total
def solve_part1():
count = 0
for bag in rules.keys():
if can_this_bag_hold_shiny_stuff(bag):
count += 1
print(count)
def solve_part2():
shiny_gold = rules['shiny gold']
print(how_many_bags_can_fit_within_this_bag(shiny_gold, 1))
if __name__ == '__main__':
for rule in ridiculous_rules:
how_many_bags_can_one_bag_carries(rule)
solve_part1()
solve_part2()