aoc2020/07/1.py

7 lines
409 B
Python
Raw Normal View History

2020-12-07 09:35:49 +00:00
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)