2020/19.py
def part1() -> int:
    """
    This is the grossest way to solve this, but I think it's one of the best ways
    because of the nice properties of the input.

    Basically, I'm constructing a regular expression from the grammar (which luckily
    already is regular for Part 1).

    I originally tried to do this intelligently with another recursive descent parser,
    but that failed miserably. I finally realized that you can just do this stupid
    method of constructing a regex and converted to that.
    """

    ans = 0

    def convert(rn) -> str:
        """
        This function converts a rule number to a regex. It uses the ``for`` loop to
        deal with the OR cases, and then joins them with "|"s. For each of the sequence
        rules, it calls itself recursively to generate a regex for the sub-rule.
        """
        parts = []
        for r in RULES[rn]:
            if isinstance(r, str):
                return r
            else:
                parts.append("".join(convert(x) for x in r))
        return "(" + "|".join(parts) + ")"

    regex = convert(0)
    for s in strs:
        if rematch(regex, s):
            ans += 1

    return ans

    This is the grossest way to solve this, but I think it's one of the best ways
    because of the nice properties of the input.

    Basically, I'm constructing a regular expression from the grammar (which luckily
    already is regular for Part 1).

    I originally tried to do this intelligently with another recursive descent parser,
    but that failed miserably. I finally realized that you can just do this stupid
    method of constructing a regex and converted to that.
    
This is the grossest way to solve this, but I think it's one of the best ways
because of the nice properties of the input.

Basically, I'm constructing a regular expression from the grammar (which luckily
already is regular for Part 1).

I originally tried to do this intelligently with another recursive descent parser,
but that failed miserably. I finally realized that you can just do this stupid
method of constructing a regex and converted to that.
This is the grossest way to solve this but I think it ' s one of the best ways because of the nice properties of the input . Basically I ' m constructing a regular expression from the grammar ( which luckily already is regular for Part 1 ) . I originally tried to do this intelligently with another recursive descent parser but that failed miserably . I finally realized that you can just do this stupid method of constructing a regex and converted to that .
def part1() -> int:
    ans = 0
    def convert(rn) -> str:
        parts = []
        for r in RULES[rn]:
            if isinstance(r, str):
                return r
            else:
                parts.append("".join(convert(x) for x in r))
        return "(" + "|".join(parts) + ")"
    regex = convert(0)
    for s in strs:
        if rematch(regex, s):
            ans += 1
    return ans
def part1 ( ) -> int : ans = 0 def convert ( rn ) -> str : """
        This function converts a rule number to a regex. It uses the ``for`` loop to
        deal with the OR cases, and then joins them with "|"s. For each of the sequence
        rules, it calls itself recursively to generate a regex for the sub-rule.
        """ parts = [ ] for r in RULES [ rn ] : if isinstance ( r , str ) : return r else : parts . append ( "" . join ( convert ( x ) for x in r ) ) return "(" + "|" . join ( parts ) + ")" regex = convert ( 0 ) for s in strs : if rematch ( regex , s ) : ans += 1 return ans
This is the grossest way to solve this, but I think it's one of the best ways
because of the nice properties of the input.
This is the grossest way to solve this but I think it ' s one of the best ways because of the nice properties of the input .
"""
    This is the grossest way to solve this, but I think it's one of the best ways
    because of the nice properties of the input.

    Basically, I'm constructing a regular expression from the grammar (which luckily
    already is regular for Part 1).

    I originally tried to do this intelligently with another recursive descent parser,
    but that failed miserably. I finally realized that you can just do this stupid
    method of constructing a regex and converted to that.
    """ """
        This function converts a rule number to a regex. It uses the ``for`` loop to
        deal with the OR cases, and then joins them with "|"s. For each of the sequence
        rules, it calls itself recursively to generate a regex for the sub-rule.
        """