From 24558f7b5b384141198d9f9a458c4c4de1fd2cec Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Wed, 29 Dec 2021 18:05:56 -0700 Subject: [PATCH] add code for day 7 --- day_7/part_1.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 day_7/part_1.py 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