From 9acbf2789a41115a7eb10825693416d13b864ba7 Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Fri, 19 Feb 2021 10:01:16 -0700 Subject: day 7 --- day7/day7.py | 33 +++++++++++++++++++++++++++++++++ day7/input_test | 7 +++++++ 2 files changed, 40 insertions(+) create mode 100644 day7/input_test (limited to 'day7') diff --git a/day7/day7.py b/day7/day7.py index 1f26537..1eb6c57 100644 --- a/day7/day7.py +++ b/day7/day7.py @@ -33,3 +33,36 @@ while True: break else: bags = new_bags + + +mapping = dict() + +# Put bags into dict structure +for section in sections: + parent, child_str = section.split(" bags contain ") + children = [] + if "no other bags" not in child_str: + for child in child_str.replace(".", "").split(", "): + count, adjective, color, _ = prog.match(child).groups() + for x in range(int(count)): + children.append(f"{adjective} {color}") + if parent not in mapping: + mapping[parent] = [] + for child in children: + mapping[parent].append(child) + +bags = list(mapping['shiny gold']) +count = 0 +new_bags = [] +while True: + new_bags = [] + for x in bags: + count += 1 + if mapping[x] == []: + continue + else: + new_bags.extend(mapping[x]) + bags = new_bags + if new_bags == []: + print(count) + break diff --git a/day7/input_test b/day7/input_test new file mode 100644 index 0000000..2723ca0 --- /dev/null +++ b/day7/input_test @@ -0,0 +1,7 @@ +shiny gold bags contain 2 dark red bags. +dark red bags contain 2 dark orange bags. +dark orange bags contain 2 dark yellow bags. +dark yellow bags contain 2 dark green bags. +dark green bags contain 2 dark blue bags. +dark blue bags contain 2 dark violet bags. +dark violet bags contain no other bags. -- cgit v1.2.3