from typing import Dict # --- Day 7: Handy Haversacks --- # You land at the regional airport in time for your next flight. In fact, it # looks like you'll even have time to grab some food: all flights are currently # delayed due to issues in luggage processing. # Due to recent aviation regulations, many rules (your puzzle input) are being # enforced about bags and their contents; bags must be color-coded and must # contain specific quantities of other color-coded bags. Apparently, nobody # responsible for these regulations considered how long they would take to # enforce! # For example, consider the following rules: # light red bags contain 1 bright white bag, 2 muted yellow bags. # dark orange bags contain 3 bright white bags, 4 muted yellow bags. # bright white bags contain 1 shiny gold bag. # muted yellow bags contain 2 shiny gold bags, 9 faded blue bags. # shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags. # dark olive bags contain 3 faded blue bags, 4 dotted black bags. # vibrant plum bags contain 5 faded blue bags, 6 dotted black bags. # faded blue bags contain no other bags. # dotted black bags contain no other bags. # These rules specify the required contents for 9 bag types. In this example, # every faded blue bag is empty, every vibrant plum bag contains 11 bags (5 # faded blue and 6 dotted black), and so on. # You have a shiny gold bag. If you wanted to carry it in at least one other # bag, how many different bag colors would be valid for the outermost bag? (In # other words: how many colors can, eventually, contain at least one shiny gold # bag?) # In the above rules, the following options would be available to you: # A bright white bag, which can hold your shiny gold bag directly. # A muted yellow bag, which can hold your shiny gold bag directly, plus # some other bags. # A dark orange bag, which can hold bright white and muted yellow bags, # either of which could then hold your shiny gold bag. # A light red bag, which can hold bright white and muted yellow bags, # either of which could then hold your shiny gold bag. # So, in this example, the number of bag colors that can eventually contain at # least one shiny gold bag is 4. # How many bag colors can eventually contain at least one shiny gold bag? (The # list of rules is quite long; make sure you get all of it.) with open("files/P7.txt", "r") as f: lines = [code for code in f.read().strip().split("\n")] # to make a dictionary of bags bag_types = [] all_bags: Dict[str, str] = {} for line in lines: main_bag = line.split(" bags contain ")[0] # get a string with the content of each bag contains = line.split(" bags contain ")[1:][0] # house keeping each_contain = contains.split(",") each_contain = [cnt.lstrip() for cnt in each_contain] # get a list of strings with the content of each bag each_contain = [" ".join(cont.split(" ")[:-1]) for cont in each_contain] # get a dictionary with bags as keys and number of them as values each_contain = { " ".join(cont.split(" ")[1:]): cont.split(" ")[0] for cont in each_contain } if main_bag not in bag_types: bag_types.append(main_bag) if all_bags.get(main_bag): each_contain.update(all_bags[main_bag]) all_bags[main_bag] = each_contain def check_bag(bags: Dict[str, str], my_bag: str, current_bag: str) -> int: if current_bag == my_bag: return 1 if bags.get(current_bag) is None: return 0 else: counts = [] for k, v in bags[current_bag].items(): counts.append(check_bag(bags, my_bag, k)) return max(counts) def part_1() -> None: found_bags = 0 my_bag = "shiny gold" for k, v in all_bags.items(): if k != my_bag: found_bags += check_bag(all_bags, my_bag, k) print(f"{found_bags} bags can contain a {my_bag} bag.") if __name__ == "__main__": part_1()