# Problem Description
The task is to implement a function that calculates a score based on a given coordinate (x, y) in a 2D matrix. The score is computed using a specific equation that involves summing the squares of certain elements in the matrix. Specifically, the score is the difference between the sum of the squares of the elements in the blue cells and the sum of the squares of the elements in the green cells. The blue and green cells are determined based on their relative positions to the given coordinate (x, y).

# Visual Facts
1. The matrix is a 2D grid of integers.
2. The coordinates (x, y) are 1-indexed.
3. The red cell is the target cell for which the score is being calculated.
4. Blue cells are located in the upper-left and lower-right diagonal directions relative to the red cell. (e.g., suppose red cell's coordinate is (x,y), then the blue cells locate at (x+i,y+i),(x+i-1,y+i-1),…(x-n,y-n) and all the cells's coordinate is inside the matrix)
5. Green cells are located in the upper-right and lower-left diagonal directions relative to the red cell.(e.g., suppose red cell's coordinate is (x,y), then the blue cells locate at (x+i,y-i),(x+i-1,y-i+1),…(x-n,y+n) and all the cells's coordinate is inside the matrix)
6. The equation for the score is: 
   Score(red cell) = Σ(blue cells^2) - Σ(green cells^2)

# Visual Patterns
1. The blue cells are diagonally adjacent to the red cell in the top-left and bottom-right directions.
2. The green cells are diagonally adjacent to the red cell in the top-right and bottom-left directions.
3. The blue cells and green cells are determined by their relative positions to the red cell, excluding the red cell itself.
4. The sum of the squares of the blue cells is subtracted by the sum of the squares of the green cells to compute the score.


# Solution
def solution(matrix: list[list[int]], x: int, y: int) -> int:
    """
    Calculates a score based on coordinates (x,y) in a 2D matrix according to the specified equation.

    Parameters:
        matrix: list[list[int]]: A 2D matrix of integers
        x: int: The x-coordinate in the matrix (1-indexed)
        y: int: The y-coordinate in the matrix (1-indexed)

    Returns:
        int: The calculated score at position (x,y)
    """
    blue_sum = 0
    green_sum = 0
    # print(f"-------------------------------------------------")
    for i in range(len(matrix)):
        if i >= 0 and i < len(matrix) and x + y - 2 - i >= 0 and x + y - 2 - i < len(matrix[0]):
            # print(f"Green: {matrix[i][x+y-2-i]}")
            green_sum += matrix[i][x+y-2-i] ** 2


    min_red = min(x, y)
    i, j = x - min_red, y - min_red
    while i < len(matrix) and j < len(matrix[0]):
        # print(f"Blue: {matrix[i][j]}")
        blue_sum += matrix[i][j] ** 2
        i += 1
        j += 1
 
    return blue_sum - green_sum


# Test Script
def test():
    test_cases = [
        ([
            [3,   2,   3,   2,  -7,  -9],
            [5,  -5,   7,  -3,   1,  -1],
            [-2,   4,  -4,   5,  -6,   8],
            [-6,   2,  -2,  -5,   4,  -8],
            [9,  -5,  -5,   1,   4,  -1],
            [-4,   6,  -4,   3,  -5,   0],
            [-7,  -3,   2,   6,  -3,   2],
            ], 4, 3, -77
        ),
        ([
            [3,   2,   3,   2,  -7,  -9],
            [5,  -5,   7,  -3,   1,  -1],
            [-2,   4,  -4,   5,  -6,   8],
            [-6,   2,  -2,  -5,   4,  -8],
            [9,  -5,  -5,   1,   4,  -1],
            [-4,   6,  -4,   3,  -5,   0],
            [-7,  -3,   2,   6,  -3,   2],
            ], 7, 1, -123
        ),
        ([
            [3,   2,   3,   2,  -7,  -9],
            [5,  -5,   7,  -3,   1,  -1],
            [-2,   4,  -4,   5,  -6,   8],
            [-6,   2,  -2,  -5,   4,  -8],
            [9,  -5,  -5,   1,   4,  -1],
            [-4,   6,  -4,   3,  -5,   0],
            [-7,  -3,   2,   6,  -3,   2],
            ], 6, 6, 82
        ),
        ([
            [3,   2,   3,   2,  -7,  -9],
            [5,  -5,   7,  -3,   1,  -1],
            [-2,   4,  -4,   5,  -6,   8],
            [-6,   2,  -2,  -5,   4,  -8],
            [9,  -5,  -5,   1,   4,  -1],
            [-4,   6,  -4,   3,  -5,   0],
            [-7,  -3,   2,   6,  -3,   2],
            ], 3, 5, -54
        ),
        ([
            [1,  -2,  3,  4],
            [5,   4,  6, -8],
            [4,   9, -4,  3],
            [7,   2, -8, -5]
        ], 3, 3, -26
        ),
        ([
            [1,  -2,  3,  4],
            [5,   4,  6, -8],
            [4,   9, -4,  3],
            [7,   2, -8, -5]
        ], 1, 4, -166
        ),
        ([
            [1,  -2,  3,  4],
            [5,   4,  6, -8],
            [4,   9, -4,  3],
            [7,   2, -8, -5]
        ], 4, 1, -133
        ),
        ([
            [3,   2,   3,   2,  -7],
            [5,  -5,   7,  -3,   1],
            [-2,   4,  -4,   5,  -6],
            [-6,   2,  -2,  -5,   4],
            [9,  -5,  -5,   1,   4],
            [-4,   6,  -4,   3,  -5]
            ],  6,  1, -55
        ),
        ([
            [3,   2,   3,   2,  -7],
            [5,  -5,   7,  -3,   1],
            [-2,   4,  -4,   5,  -6],
            [-6,   2,  -2,  -5,   4],
            [9,  -5,  -5,   1,   4],
            [-4,   6,  -4,   3,  -5]
            ],  2,  5, -66
        ),
        ([
            [3,   2,   3,   2,  -7],
            [5,  -5,   7,  -3,   1],
            [-2,   4,  -4,   5,  -6],
            [-6,   2,  -2,  -5,   4],
            [9,  -5,  -5,   1,   4],
            [-4,   6,  -4,   3,  -5]
            ],  1,  4, -100
        ),
        
    ]

    for matrix, x, y, expected_output in test_cases:
        try:
            output = solution(matrix, x, y)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`matrix`:\n"
            )
            # Format matrix for better readability
            for row in matrix:
                error_message += f"{row}\n"
            error_message += (
                f"`x`: {x}\n"
                f"`y`: {y}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`matrix`:\n"
            )
            # Format matrix for better readability
            for row in matrix:
                error_message += f"{row}\n"
            error_message += (
                f"`x`: {x}\n"
                f"`y`: {y}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()