14 for line in open(filename, "r"):
21 (rule_number,rule) = line.split(":")
22 rule_number=int(rule_number)
24 rules[rule_number] = [s.strip() for s in rule.split("|")]
28 def get_rule_regex(rulenumber):
31 for rule in rules[rulenumber]:
33 # not a character, so we're going to go down the wishing well
34 for number in rule.split(" "):
36 regex+=get_rule_regex(number)
46 # ok - so all the rules are in, lets build a huge regex that gets all the rules
47 for rule_number in rules:
48 print(str(rule_number) + ": " + "| ".join(rules[rule_number]))
50 pattern="^"+get_rule_regex(0)+"$"
55 if re.search(pattern, line):
56 print("Line: {} matches".format(line))
59 print("There were {} matches in total".format(match_count))