summaryrefslogtreecommitdiff
path: root/day7/day7.py
diff options
context:
space:
mode:
Diffstat (limited to 'day7/day7.py')
-rw-r--r--day7/day7.py33
1 files changed, 33 insertions, 0 deletions
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