# Problem Description
The task is to calculate the "U value" for a given rectangle in a list of rectangles. The U value is determined by the heights of the rectangles and their positions. Specifically, the U value for a rectangle at a given index is calculated based on the heights of the rectangles adjacent to it. The goal is to implement a function that takes a list of rectangle heights and a target index, and returns the U value for the rectangle at that index.

# Visual Facts
1. The input is a list of integers representing the heights of rectangles.
2. The rectangles are indexed from 0 to n-1.
3. The U value is calculated for a specific rectangle at a given index.
4. The U value is determined by the heights of the rectangle at the target index and its adjacent rectangles.
5. The U value is the product of the height of the rectangle at the target index and the sum of the heights of its adjacent rectangles.
6. The image shows examples of U values for rectangles at different indices.

# Visual Patterns
1. The U value for a rectangle at index `i` is calculated as follows:
   - For the rectangle at index `i`, consider the heights of the rectangles at indices `i-1`and `i`.
   - If `i` is the first rectangle (index 0), the left adjacent rectangle is the rightmost rectangle (index n-1)
2. Formula Pattern:
   - The U value appears to be calculated as: U = min(current_height, left_height) * 2
   - For example: At index 0 in first example:
     * Heights are 8 (index 0) and 5 (index 4, the rightmost rectangle)
     * min(8,5) = 5
     * U value = 5 * 2 = 10

# 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)
    width = 2
    height = min(rectangles[i], rectangles[j])
    return height * width


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

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

    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()