summaryrefslogtreecommitdiff
path: root/day7
diff options
context:
space:
mode:
authorCody Hiar <cody@hiar.ca>2021-02-19 10:01:16 -0700
committerCody Hiar <cody@hiar.ca>2021-02-19 10:01:16 -0700
commit9acbf2789a41115a7eb10825693416d13b864ba7 (patch)
tree363e75a90b01d168e2230e92ecbef174591c05d0 /day7
parent3bb23093ae15aaa2b11c3b3d771d6a9e42b73a0c (diff)
day 7
Diffstat (limited to 'day7')
-rw-r--r--day7/day7.py33
-rw-r--r--day7/input_test7
2 files changed, 40 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
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.