2020 day7/python: rename functions
This commit is contained in:
parent
05e0930822
commit
70cc669fbc
1 changed files with 6 additions and 6 deletions
|
@ -27,24 +27,24 @@ def process_rules(rules):
|
|||
|
||||
return rules_dict
|
||||
|
||||
def can_contain(rules, colour):
|
||||
def parents_of(rules, colour):
|
||||
parents = set()
|
||||
for parent, children in rules.items():
|
||||
if colour in children:
|
||||
parents.add(parent)
|
||||
parents |= can_contain(rules, parent)
|
||||
parents |= parents_of(rules, parent)
|
||||
return parents
|
||||
|
||||
def colour_contains(rules, colour):
|
||||
def children_of(rules, colour):
|
||||
children = defaultdict(lambda: 0)
|
||||
for child_colour, child_num in rules[colour].items():
|
||||
children[child_colour] += child_num
|
||||
for col, num in colour_contains(rules, child_colour).items():
|
||||
for col, num in children_of(rules, child_colour).items():
|
||||
children[col] += child_num * num
|
||||
return children
|
||||
|
||||
if __name__ == '__main__':
|
||||
bag_rules = [rule.strip() for rule in sys.stdin.readlines()]
|
||||
rules = process_rules(bag_rules)
|
||||
print(len(can_contain(rules, 'shiny gold')))
|
||||
print(sum(colour_contains(rules, 'shiny gold').values()))
|
||||
print(len(parents_of(rules, 'shiny gold')))
|
||||
print(sum(children_of(rules, 'shiny gold').values()))
|
||||
|
|
Loading…
Reference in a new issue