7 lines
383 B
Python
7 lines
383 B
Python
|
import re
|
||
|
lines = [r for r in open("input", "r").read().split("\n") if r]
|
||
|
baglines = [l.split(" bags contain ") for l in lines]
|
||
|
rules = {bag: [re.match("(\d) (.*) bag", c).groups() for c in contains.split(", ") if c != "no other bags."] for (bag, contains) in baglines}
|
||
|
total = lambda b: sum([int(count) * total(bag) for (count, bag) in rules[b]]) + 1
|
||
|
print(total("shiny gold") - 1)
|