コード例 #1
0
def test_one_rule_or():
    test_input = '''0: 1 | 2
1: "x"
2: "y"'''

    expected = '^(x|y)$'
    assert expected == grammar_to_regexp(test_input)
コード例 #2
0
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)
コード例 #3
0
def test_a_single_rule_is_just_followed():
    test_input = '''0: 1
1: "c"'''

    expected = "^c$"

    assert expected == grammar_to_regexp(test_input)
コード例 #4
0
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)
コード例 #5
0
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)
コード例 #6
0
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)
コード例 #7
0
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)
コード例 #8
0
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.")
コード例 #9
0
def test_single_character_becomes_single_character(c):
    test_input = f'0: "{c}"'
    expected = f"^{c}$"

    assert expected == grammar_to_regexp(test_input)