2020/Day 7/one.py

43 lines
897 B
Python
Raw Permalink Normal View History

2022-07-01 15:56:12 +02:00
#!/bin/python
def check(bag, rules):
if "shiny gold" in rules[bag]:
return True
result = False
for possible_bag in rules[bag]:
result = result or check (possible_bag, rules)
return result
# $color bags contain {$number $color bags,}*x
# color: {colors}
rules = {}
with open("input", 'r') as input:
for line in input:
line = line.strip()
parts = line.split(" bags contain ")
key = parts[0]
value = []
parts = parts[1].strip(".").split(", ")
for part in parts:
if part == "no other bags":
break;
value.append(part.split(" ", 1)[1].replace("bags", "").replace("bag", "").strip())
rules[key] = value
total = 0
for key in rules:
print(f"Now checking: {key}")
if check(key, rules):
total += 1
print(total)