diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-12-08 06:27:07 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-12-08 06:27:21 +0100 |
| commit | c9cc3b96b3c85cdd118c01b74a8cee554b0dd7e9 (patch) | |
| tree | f1493a6a5784a7044b7d7a625c44717ed0b39e49 /20 | |
| parent | 24442bf71850913703b59804e0479e8ff58c1721 (diff) | |
| download | aoc-c9cc3b96b3c85cdd118c01b74a8cee554b0dd7e9.tar.gz | |
7: bag -> node
Diffstat (limited to '20')
| -rw-r--r-- | 20/py/d07.py | 42 |
1 files changed, 20 insertions, 22 deletions
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__": |
