aboutsummaryrefslogtreecommitdiff
path: root/2020/07/puzzle-1.py
blob: da054b8dbcff3b2b8539295e5eb891c3caca0dd2 (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
36
37
38
#!/usr/bin/env python3


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

# Recursively check if a bag can hold a shiny gold bag
def holds_bag(innerbags: list[str]) -> bool:
	if "no other" in innerbags:
		return False
	elif "shiny gold" in innerbags:
		return True

	for subbag in innerbags:
		if holds_bag(bdict[subbag]):
			return True


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

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

		# { bag_name: [contained_bag_1, containted_bag_2, ...] }
		bdict[data[0]] = [" ".join(b.split(" ")[-3:][:2]) for b in data[1].split(",")]

	count = 0
	for bag in bdict:
		if holds_bag(bdict[bag]):
			count += 1

	print(count)


if __name__ == "__main__":
	main()