def test_count_bags_that_can_contain_shiny_gold():
    example_path = os.path.join(ROOT_DIR,
                                "tests/day_7/example_bag_rules_1.csv")
    example_rules = get_bag_rules(example_path)
    with patch("day_7.bag_rules_pt1.get_bag_rules",
               return_value=example_rules):
        assert count_bags_that_can_contain_shiny_gold() == 4
def test_count_bags_contained_in_bag():
    example_path = os.path.join(ROOT_DIR,
                                "tests/day_7/example_bag_rules_2.csv")
    example_rules = get_bag_rules(example_path)
    with patch("day_7.bag_rules_pt2.get_bag_rules",
               return_value=example_rules):
        assert count_bags_contained_in_bag("shiny gold") == 126
Example #3
0
def count_bags_that_can_contain_shiny_gold() -> int:
    """
    count how many distinct bag colours can ultimately contain at least shiny gold bag, excluding shiny gold itself
    :return: distinct count of bag colours
    """
    all_rules = get_bag_rules()
    return len({
        colour
        for colour in all_rules.keys() if colour != "shiny gold"
        and Bag(colour, all_rules).can_contain_shiny_gold()
    })
Example #4
0
def count_bags_contained_in_bag(colour: str) -> int:
    """
    count how many bags must be contained within a given colour of bag.
    recursively checks the colours contained within
    :param colour: colour of the bag to check
    :return: total bags contained within
    """
    bag = Bag(colour, get_bag_rules())
    counter = 0
    if bag.contents:
        for other_bag_colour, quantity in bag.contents.items():
            counter += quantity * (
                1 + count_bags_contained_in_bag(other_bag_colour))
    else:
        pass
    return counter
Example #5
0
def test_get_bag_rules():
    example_path = os.path.join(ROOT_DIR,
                                "tests/day_7/example_bag_rules_1.csv")
    actual = get_bag_rules(example_path)
    expected = {
        "light red":
        "light red bags contain 1 bright white bag, 2 muted yellow bags.\n",
        "dark orange":
        "dark orange bags contain 3 bright white bags, 4 muted yellow bags.\n",
        "bright white": "bright white bags contain 1 shiny gold bag.\n",
        "muted yellow":
        "muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.\n",
        "shiny gold":
        "shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.\n",
        "dark olive":
        "dark olive bags contain 3 faded blue bags, 4 dotted black bags.\n",
        "vibrant plum":
        "vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.\n",
        "faded blue": "faded blue bags contain no other bags.\n",
        "dotted black": "dotted black bags contain no other bags.",
    }
    assert actual == expected
Example #6
0
        "muted yellow":
        "muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.\n",
        "shiny gold":
        "shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.\n",
        "dark olive":
        "dark olive bags contain 3 faded blue bags, 4 dotted black bags.\n",
        "vibrant plum":
        "vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.\n",
        "faded blue": "faded blue bags contain no other bags.\n",
        "dotted black": "dotted black bags contain no other bags.",
    }
    assert actual == expected


EXAMPLE_PATH = os.path.join(ROOT_DIR, "tests/day_7/example_bag_rules_1.csv")
EXAMPLE_RULES = get_bag_rules(EXAMPLE_PATH)
BAG = Bag("shiny gold", EXAMPLE_RULES)


def test_bag_all_rules():
    assert BAG.all_rules == EXAMPLE_RULES


def test_bag_colour():
    assert BAG.colour == "shiny gold"


def test_bag_contents():
    assert BAG.contents == {"dark olive": 1, "vibrant plum": 2}