summaryrefslogtreecommitdiff
path: root/day7/day7.py
diff options
context:
space:
mode:
authorCody Hiar <cody@hiar.ca>2021-02-05 10:43:34 -0700
committerCody Hiar <cody@hiar.ca>2021-02-05 10:43:34 -0700
commit3bb23093ae15aaa2b11c3b3d771d6a9e42b73a0c (patch)
treeaf03e0bb986a3bff0c76d5dfe9a949ac3b942e54 /day7/day7.py
parentb4a3bd52e5e610f431d9aa49e3ac0af918e1e344 (diff)
day 7 part 1
Diffstat (limited to 'day7/day7.py')
-rw-r--r--day7/day7.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/day7/day7.py b/day7/day7.py
new file mode 100644
index 0000000..1f26537
--- /dev/null
+++ b/day7/day7.py
@@ -0,0 +1,35 @@
+"""Day 7."""
+import re
+from collections import defaultdict
+
+pattern = r"(\d+) ([a-z]+) ([a-z]+) (bag[s]?)"
+prog = re.compile(pattern)
+
+with open("input") as f:
+ sections = f.read().rstrip().split("\n")
+
+mapping = defaultdict(lambda: [])
+
+# 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(", "):
+ _, adjective, color, _ = prog.match(child).groups()
+ children.append(f"{adjective} {color}")
+ for child in children:
+ mapping[child].append(parent)
+
+# Part 1
+bags = set(mapping['shiny gold'])
+while True:
+ new_bags = bags.copy()
+ for bag in bags:
+ for new_bag in mapping[bag]:
+ new_bags.add(new_bag)
+ if len(new_bags) == len(bags):
+ print(len(bags))
+ break
+ else:
+ bags = new_bags