aboutsummaryrefslogtreecommitdiff
path: root/2020/07/puzzle-2.py
blob: c87ea1399411fb860e0f1ff52b54bdf38b010c2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3


bdict: dict[str, list[str]] = {}

# Recursively find the number of sub-bags in a bag
def total_bags(innerbags: list[dict[int, str]]) -> int:
	if innerbags[0]["name"] == "no other":
		return 0

	return sum([bag["count"] + bag["count"] * total_bags(bdict[bag["name"]]) for bag in innerbags])


def main() -> None:
	with open("input", "r") as f:
		lines = f.readlines()

	for baginfo in lines:
		data = baginfo.split(" bags contain")

		# { bag_name: [{ count: n, name: bag_name_ }, ...] }
		bdict[data[0]] = []
		for b in data[1].split(","):
			bdict[data[0]].append(
				{
					"count": int(b.replace("no", "0").split(" ")[1]),
					"name": " ".join(b.split(" ")[-3:][:2]),
				}
			)

	print(total_bags(bdict["shiny gold"]))


if __name__ == "__main__":
	main()