aoc2020/07/2.py

7 lines
383 B
Python
Raw Normal View History

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