Browse Source

day7/python: rename functions

main
Xiretza 1 year ago
parent
commit
ebf0723b48
Signed by: xiretza
GPG Key ID: 17B78226F7139993
  1. 12
      day7/day7.py

12
day7/day7.py

@ -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…
Cancel
Save