0
0
Fork 0

add code for day 7

This commit is contained in:
Oliver-Akins 2021-12-29 18:05:56 -07:00
parent 64b814e453
commit 24558f7b5b

30
day_7/part_1.py Normal file
View file

@ -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<colour>[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)