Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_contents(s):
- contents = {}
- for line in s.strip('\n').split('\n'):
- outer, inners = line.split(' bags contain ')
- contents[outer] = {}
- for match in re.finditer(r'(\d+) (.*?) bags?[,.]', inners):
- n, inner = match.groups()
- contents[outer][inner] = int(n)
- return contents
- def count_containers(s, query='shiny gold'):
- contents = get_contents(s)
- def valid(bag):
- return any(inner == query or valid(inner) for inner in contents[bag])
- return sum(valid(bag) for bag in contents)
- def count_total_inside(s, query='shiny gold'):
- contents = get_contents(s)
- def count_inside(bag):
- return sum(n * (count_inside(b) + 1) for b, n in contents[bag].items())
- return count_inside(query)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement