diff --git a/day_7/part_1.py b/day_7/part_1.py new file mode 100644 index 0000000..4511a8f --- /dev/null +++ b/day_7/part_1.py @@ -0,0 +1,30 @@ +import re + +with open("test") as file: + rules = file.read().split("\n") + + +bag_colours = {} + + +for rule in rules: + container, containing = rule.split(" bags contain ", 1) + nested = containing.replace(".", "").split(", ") + + for colour in nested: + + # End iteration if no other bags can be nested internally + if colour == "no other bags": + break + + print(colour) + colour = re.match(r"^[0-9]+ (?P[A-Za-z ]+) bags?$", colour).group("colour") + + # Can nest other colours inside of the bag + if colour not in bag_colours: + bag_colours[colour] = [container] + else: + if container not in bag_colours[colour]: + bag_colours[colour].append(container) + +print(bag_colours) \ No newline at end of file