7 lines
409 B
Python
7 lines
409 B
Python
import re
|
|
lines = [l for l in open("input", "r").read().split("\n") if l]
|
|
baglines = [l.split(" bags contain ") for l in lines]
|
|
rules = {bag: [re.match("(\d) (.*) bag", c).group(2) for c in contains.split(", ") if c != "no other bags."] for (bag, contains) in baglines}
|
|
contain = lambda b: {b}.union(*[contain(bag) for (bag, contains) in rules.items() if b in contains])
|
|
print(len(contain("shiny gold")) - 1)
|