def test_one_rule_or(): test_input = '''0: 1 | 2 1: "x" 2: "y"''' expected = '^(x|y)$' assert expected == grammar_to_regexp(test_input)
def test_two_rules_are_correctly_expanded(): test_input = '''0: 1 2 1: "a" 2: "f"''' expected = "^af$" assert expected == grammar_to_regexp(test_input)
def test_a_single_rule_is_just_followed(): test_input = '''0: 1 1: "c"''' expected = "^c$" assert expected == grammar_to_regexp(test_input)
def test_start_from_different_root(): test_input = '''0: 1 | 2 1: "a" 2: 3 | 4 3: "b" 4: "c"''' expected = '^(b|c)$' assert expected == grammar_to_regexp(test_input, root=2)
def test_nested_or(): test_input = '''0: 1 | 2 1: "a" 2: 3 | 4 3: "b" 4: "c"''' expected = '^(a|(b|c))$' assert expected == grammar_to_regexp(test_input)
def test_two_rule_or(): test_input = '''0: 1 2 | 3 4 1: "a" 2: "b" 3: "x" 4: "y"''' expected = '^(ab|xy)$' assert expected == grammar_to_regexp(test_input)
def test_down_the_rabbithole(): test_input = '''0: 1 2 1: 3 2: 4 3: "c" 4: "u"''' expected = "^cu$" assert expected == grammar_to_regexp(test_input)
def main(): input_str = get_data(day=19) grammar, messages = read_data(input_str) regexp = grammar_to_regexp(grammar) pattern = re.compile(regexp) num_match = sum(check_message(pattern, message) for message in messages) print(f"There's {num_match} messages matching the rule.") res = part2(grammar, messages) print(f"There's now {res} matching messages with the new rules.")
def test_single_character_becomes_single_character(c): test_input = f'0: "{c}"' expected = f"^{c}$" assert expected == grammar_to_regexp(test_input)