-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpartTwo.py
More file actions
29 lines (22 loc) · 971 Bytes
/
Copy pathpartTwo.py
File metadata and controls
29 lines (22 loc) · 971 Bytes
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
from common import *
from typing import Dict
def count_for_bag(ruleset: Dict[str, Dict[str, int]], test_cl: str) -> int:
if len(ruleset[test_cl]) == 0:
return -1
count = 0
for child_colour in ruleset[test_cl]:
child_bags = count_for_bag(ruleset, child_colour)
if child_bags == -1:
v = ruleset[test_cl][child_colour]
else:
v = ruleset[test_cl][child_colour] * child_bags
# The below line includes the number of bags that contain all the children - for
# example, if you had a bag type that had 6 child bags, and there were 3 of these
# master bags, the line above would add all the child bags (6*3 of them), and the line
# below would add the 3 container bags.
v += ruleset[test_cl][child_colour]
count += v
return count
def partTwo(instr: str) -> int:
rules = parse(instr)
return count_for_bag(rules, target_colour)