# Problem Description:
The problem involves calculating a specific value, referred to as the "X value," between two adjacent rectangles in a list of rectangle heights. The function takes in a list of integers representing the heights of these rectangles and an index position. The goal is to compute the X value for the rectangle at the given index position. The X value is defined as the difference between the heights of the rectangle at the given index and the rectangle at the next index.

# Visual Facts:
1. The input is a list of rectangle heights.
2. The index positions of the rectangles are labeled from 0 to 4.
3. The X value is calculated for a specific rectangle at a given index.
4. The X value is the difference between the height of the rectangle at the given index and the height of the rectangle at the next index.
5. The image shows multiple examples of how the X value is calculated for different indices.

# Visual Patterns:
1. The X value is always a positive integer.
2. The X value is calculated as the absolute difference between the heights of two adjacent rectangles.
3. For a given index `i`, the next index is `(i + 1) % len(rectangles)`, ensuring circular indexing.
4. The X value for a rectangle at index `i` is given by `|rectangles[i] - rectangles[(i + 1) % len(rectangles)]|`.


# Solution
from typing import List, Tuple

def solution(rectangles: List[int], target_idx: int) -> int:
    """
    Calculate the U value between two adjacent rectangles.

    Args:
        rectangles: A list of integers representing rectangle heights from left to right.
        target_idx: The 0-based index position at which to calculate the U value.  

    Returns:
        The U value between rectangles at target_idx.     
    """
    i = target_idx
    j = (target_idx + 1) % len(rectangles)
    height = min(rectangles[i], rectangles[j])
    return max(rectangles[i], rectangles[j])-height


# Test Script
def test():
    test_cases = [
        {
            "rectangles": [8, 6, 2, 3, 5],
            "target_idx": 0,
            "expected": 2
        },

        {
            "rectangles": [8, 6, 2, 3, 5],
            "target_idx": 1,
            "expected": 4
        },
        {
            "rectangles": [8, 6, 2, 3, 5],
            "target_idx": 3,
            "expected": 2
        },
        {
            "rectangles": [8, 6, 2, 3, 5],
            "target_idx": 2,
            "expected": 1
        },
        {
            "rectangles": [10, 5],
            "target_idx": 0,
            "expected": 5
        },
        {
            "rectangles": [1, 2, 3, 4, 5],
            "target_idx": 0,
            "expected": 1
        },
        {
            "rectangles": [1, 2, 3, 4, 5],
            "target_idx": 1,
            "expected": 1
        },
        {
            "rectangles": [5, 5, 5, 5, 5, 5],
            "target_idx": 2,
            "expected": 0
        },
        {
            "rectangles": [5, 5, 5, 5, 5, 5],
            "target_idx": 1,
            "expected": 0
        },
        {
            "rectangles": [2, 8, 5, 5, 5, 5],
            "target_idx": 5,
            "expected": 3
        },
    ]

    for i, tc in enumerate(test_cases, start=1):
        rectangles = tc["rectangles"]
        target_idx = tc["target_idx"] 
        expected_output = tc["expected"]

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

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

test()