import re


def parse_garbage(text, i):
    """
    Scans garbage, returning the text position after the end of the garbage.
    Now also returns the garbage's "score" - the # of characters, not counting
    the opening/closing <> and any !'s or characters they cancel.

    The i parameter is the text position *after* the opening <
    """
    score = 0
    # Loop 'til the closing > is found.
    while True:
        m = re.match(r"[^!>]*[!>]", text[i:])
        if not m:
            raise ValueError("Bad syntax (after position {}) in garbage: Expecting >".format(i))
        delta = m.end(0) - m.start(0)
        i += delta
        score += delta
        if text[i-1] == ">":
            return (i, score - 1)
        # If we get here, we hit a ! - skip it and the char following.
        i += 1
        score -= 1