From d5996aed9129a7beac80585e1c755708e8d81fc0 Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 7 Dec 2020 16:58:58 +0100 Subject: [PATCH] Day 7 --- 2020/day7.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2020/day7.py diff --git a/2020/day7.py b/2020/day7.py new file mode 100644 index 0000000..7207efb --- /dev/null +++ b/2020/day7.py @@ -0,0 +1,43 @@ +import re +from collections import defaultdict + + +LINE_RE = re.compile(r"^(\w+ \w+) bags contain (.+)\.$") +CONT_RE = re.compile(r"(\d+) (\w+ \w+) bags?") + + +def main(): + with open("day7.txt", "rt") as f: + lines = [line.rstrip() for line in f.readlines()] + rules = defaultdict(dict) + for line in lines: + match = LINE_RE.match(line) + subject, contained = match.groups() + contained = contained.split(", ") + for c in contained: + match = CONT_RE.match(c) + if match: + cn, ct = match.groups() + rules[subject][ct] = int(cn) + + # Part 1 + num_containers = sum(contain_shiny_gold(bag, rules) for bag in rules.copy()) + print("Containing at least 1 shiny gold:", num_containers) + + # Part 2 + inside_shiny_gold = count_bags("shiny gold", rules) - 1 + print("Inside shiny gold:", inside_shiny_gold) + + +def contain_shiny_gold(bag, rules): + if "shiny gold" in rules[bag]: + return True + return any(contain_shiny_gold(b, rules) for b in rules[bag]) + + +def count_bags(bag, rules): + return sum(count_bags(b, rules) * rules[bag][b] for b in rules[bag]) + 1 + + +if __name__ == "__main__": + main()