Solution to problem 7 part 2 in Python (failed mypy:()
This commit is contained in:
66
src/P7.py
66
src/P7.py
@@ -98,5 +98,71 @@ def part_1() -> None:
|
|||||||
print(f"{found_bags} bags can contain a {my_bag} bag.")
|
print(f"{found_bags} bags can contain a {my_bag} bag.")
|
||||||
|
|
||||||
|
|
||||||
|
# --- Part Two ---
|
||||||
|
|
||||||
|
# It's getting pretty expensive to fly these days - not because of ticket
|
||||||
|
# prices, but because of the ridiculous number of bags you need to buy!
|
||||||
|
|
||||||
|
# Consider again your shiny gold bag and the rules from the above example:
|
||||||
|
|
||||||
|
# faded blue bags contain 0 other bags.
|
||||||
|
# dotted black bags contain 0 other bags.
|
||||||
|
# vibrant plum bags contain 11 other bags: 5 faded blue bags and 6 dotted
|
||||||
|
# black bags.
|
||||||
|
# dark olive bags contain 7 other bags: 3 faded blue bags and 4 dotted
|
||||||
|
# black bags.
|
||||||
|
|
||||||
|
# So, a single shiny gold bag must contain 1 dark olive bag (and the 7 bags
|
||||||
|
# within it) plus 2 vibrant plum bags (and the 11 bags within each of those):
|
||||||
|
# 1 + 1*7 + 2 + 2*11 = 32 bags!
|
||||||
|
|
||||||
|
# Of course, the actual rules have a small chance of going several levels
|
||||||
|
# deeper than this example; be sure to count all of the bags, even if the
|
||||||
|
# nesting becomes topologically impractical!
|
||||||
|
|
||||||
|
# Here's another example:
|
||||||
|
|
||||||
|
# shiny gold bags contain 2 dark red bags.
|
||||||
|
# dark red bags contain 2 dark orange bags.
|
||||||
|
# dark orange bags contain 2 dark yellow bags.
|
||||||
|
# dark yellow bags contain 2 dark green bags.
|
||||||
|
# dark green bags contain 2 dark blue bags.
|
||||||
|
# dark blue bags contain 2 dark violet bags.
|
||||||
|
# dark violet bags contain no other bags.
|
||||||
|
|
||||||
|
# In this example, a single shiny gold bag must contain 126 other bags.
|
||||||
|
|
||||||
|
# How many individual bags are required inside your single shiny gold bag?
|
||||||
|
|
||||||
|
|
||||||
|
bags_contains: Dict[str, str] = {}
|
||||||
|
for k, v in all_bags.items():
|
||||||
|
bags_contains[k] = []
|
||||||
|
try:
|
||||||
|
for kk, vv in v.items():
|
||||||
|
bags_contains[k] += [kk] * int(vv)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def count_bags(current_bag: str) -> int:
|
||||||
|
if current_bag == " " or bags_contains.get(current_bag) is None:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
cnt = len(bags_contains[current_bag])
|
||||||
|
nbags = []
|
||||||
|
for k in bags_contains[current_bag]:
|
||||||
|
nbags.append(count_bags(k))
|
||||||
|
return sum(nbags) + cnt
|
||||||
|
|
||||||
|
|
||||||
|
def part_2() -> None:
|
||||||
|
my_bag = "shiny gold"
|
||||||
|
nbags = count_bags(my_bag)
|
||||||
|
|
||||||
|
print(f"My shiny gold bag can contain {nbags} bags")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
part_1()
|
part_1()
|
||||||
|
part_2()
|
||||||
|
|||||||
Reference in New Issue
Block a user