From c9cc3b96b3c85cdd118c01b74a8cee554b0dd7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 8 Dec 2020 06:27:07 +0100 Subject: 7: bag -> node --- 20/py/d07.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to '20/py') diff --git a/20/py/d07.py b/20/py/d07.py index 0252e6e..fc39ad0 100644 --- a/20/py/d07.py +++ b/20/py/d07.py @@ -17,44 +17,42 @@ class Node: return self.name -def graph(bags): +def graph(nodes): print("digraph G {") print("rankdir=\"LR\";") - for bag in bags.values(): - for child in bag.children: - print(f"\"{bag.name}\" -> \"{child[0].name}\" [ label=\"{child[1]}\" ];") + for node in nodes.values(): + for child in node.children: + print(f"\"{node.name}\" -> \"{child[0].name}\" [ label=\"{child[1]}\" ];") print("}") def parse(_in): - bags = {} # bag: node - for bag in _in: - match = re.match(r"(\w+ \w+) bags contain (no other bags|(((\d+) (\w+ \w+)) bags?(, )?)+)\.", - bag) - children = [(kind, int(count)) for count, kind in zip(match.captures(5), - match.captures(6))] - bags[match[1]] = Node(match[1], children) - for name, bag in bags.items(): - bags[name].children = [(bags[bag], amount) for bag, amount in bags[name].children] - return bags + nodes = {} + for line in _in: + match = re.match(r"(\w+ \w+) bags contain (no other bags|(((\d+) (\w+ \w+)) bags?(, )?)+)\.", line) + children = [(node_str, int(amount)) for amount, node_str in zip(match.captures(5), match.captures(6))] + nodes[match[1]] = Node(match[1], children) + for node in nodes.values(): + node.children = [(nodes[node_str], amount) for node_str, amount in node.children] + return nodes def pt1(_in): @functools.cache - def can_contain(bag, target): - return target in bag or any(can_contain(child, target) for child in bag) + def can_contain(node, target): + return target in node or any(can_contain(child, target) for child in node) - bags = parse(_in) - return len([bag for bag in bags.values() if can_contain(bag, bags["shiny gold"])]) + nodes = parse(_in) + return len([node for node in nodes.values() if can_contain(node, nodes["shiny gold"])]) def pt2(_in): @functools.cache - def count_children(bag): - return 1 + sum([child[1] * count_children(child[0]) for child in bag.children]) + def count_children(node): + return 1 + sum([child[1] * count_children(child[0]) for child in node.children]) - bags = parse(_in) - return count_children(bags["shiny gold"]) - 1 + nodes = parse(_in) + return count_children(nodes["shiny gold"]) - 1 if __name__ == "__main__": -- cgit v1.2.1