# Problem Description:
The problem involves determining how many rats a cat can catch given the cat's position and the positions of the rats on a linear grid.   The cat can only catch rats that are within a certain range and not blocked by empty spaces.   The task is to implement a function that calculates the number of rats the cat can catch based on these conditions.

# Visual Facts:
1. The grid is represented as a series of cells indexed from 0 to 10.
2. Each cell can either be empty, contain a rat, or contain a cat.
3. The cat is represented by a silhouette of a cat.
4. Rats are represented by silhouettes of rats.
5. The cat can catch rats within a certain range, as indicated by green arrows.
6. The cat cannot catch rats if there are empty cells between the cat and the rats, as indicated by red crosses.

# Visual Patterns:
1. **Range of Capture**: The cat can catch rats if rats are consecutively placed near the cat.
2. **Blocking Condition**: If there is an empty cell between the cat and a rat within the capture range, the cat cannot catch that rat.
3. **Counting Captured Rats**: The number of rats captured is the sum of all rats within the capture range that are not blocked by empty cells.
4. given the index `i` of a cat, the rats it can catch are the ones that have continuous indices from the cat's index. like i-1, i-2, i-3,...i+1, i+2, i+3, ... Any gap between the cat and the rat will prevent the cat from catching the rat.

# Solution
from typing import List

def solution(cat: int, rats: List[int]) -> int:
    """
    Given a cat's position and a list of rats positions, returns the number of rats the cat can catch.

    Parameters:
        cat (int): The position index of the cat (0-based)
        rats (List[int]): List of position indices for the rats (0-based)

    Returns:
        int: Number of rats that can be caught by the cat
    """
    idx = cat
    count = 0
    # move rightward 

    idx+=1
    while idx <= max(rats) and idx in rats:
        count+=1
        idx += 1
    # move leftward 
    idx = cat-1
    while idx >= 0 and idx in rats:
        count+=1
        idx -= 1

    return count


# Test Script
def test():
    test_cases = [
        (5,[0,1,3,6,7,10],2),
        (4,[0,1,2,6,7,8,9,10], 0),
        (7,[0,1,2,3,4,5,6,8,9,10],10),
        (3,[0,1,2,4,5,6,7,8,9,10], 10),
        (5,[0,1,2,3,10], 0),
        (8,[0,1,2,3,4,5,6,7,10], 8),
        (4,[0,2,5,8,3], 3),
        (2,[0,1,7,8,9,10], 2),   
        (1,[0,2,3,4,5,6,7,8,9,10], 10),
        (6,[0,1,2,3,4,5,7,8,9,10], 10)
        
    ]

    for cat, rats, expected_output in test_cases:
        try:
            output = solution(cat, rats)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`cat`: {cat}\n"
                f"`rats`: {rats}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`cat`: {cat}\n" 
                f"`rats`: {rats}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()