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 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'day7/day7.py') 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 -- cgit v1.2.3