# Problem Description
This is a string processing problem where we need to calculate the total cost of forming a target string using a circular disk mechanism. The disk contains lowercase letters (a-z) and a special 'check' character arranged in a circle. We need to select each character of the target string by rotating to it and then confirming by rotating back to 'check', accumulating costs for all rotations.

# Visual Facts
1. The disk has 27 positions (26 letters + 1 'check' character)
2. Letters are arranged alphabetically clockwise from 'a' to 'z'
3. The 'check' character is positioned between 'a' and 'z'
4. Two examples are provided:
   - Example 1: target="h", total cost=16 (8+8)
   - Example 2: target="jay", total cost=26 (10+10+1+1+2+2)
5. Each move shows two steps:
   - Step 1: From 'check' rotate to target letter
   - Step 2: Rotate back to 'check'
6. The pointer always starts at 'check' position
7. Movement can be either clockwise or counterclockwise

# Visual Patterns
1. Movement Cost Pattern:
   - Cost is always the minimum number of steps needed to reach the target
   - Can move in either direction (clockwise or counterclockwise)
   - For each letter, total cost = (cost to letter + cost back to check) × 2

2. Operation Pattern:
   - For each character in target string:
     1. Move from 'check' to character (cost X)
     2. Move back to 'check' (cost X)
     3. Repeat for next character

3. Cost Calculation Pattern:
   - Example 1: "h" costs 8 steps to reach + 8 steps back = 16
   - Example 2: "jay" breaks down as:
     * j: 10 steps each way = 20
     * a: 1 step each way = 2
     * y: 2 steps each way = 4
     * Total = 26

4. Optimization Pattern:
   - Always choose the shorter path (clockwise or counterclockwise)
   - The cost for each step is the minimum distance between current position and target position on the circular disk

# Solution
def solution(target: str) -> int:
    """
    Calculates the cost of a target string according to defined rules.

    Args:
        target: The input string to calculate cost for

    Returns:
        The total cost as an integer
    """
    # Initialize total cost
    total_cost = 0

    # Start from the initial position 'a'
    current_position = '0'

    # Define helper function to calculate distance
    def calculate_distance(from_char: str, to_char: str) -> int:
        """
        Calculate the minimum distance between two positions on the circular wheel.
        """
        # Distance considering the alphabet plus the "check" position
        positions = "abcdefghijklmnopqrstuvwxyz0"
        from_index = positions.index(from_char)
        to_index = positions.index(to_char)
        return min(abs(from_index - to_index), len(positions) - abs(from_index - to_index))

    # Iterate over each character in the input string
    for char in target:
        # Move to the target letter
        total_cost += calculate_distance(current_position, char)
        
        # Move to the "check" position
        total_cost += calculate_distance(char, "0")
        
        # Update the current position to "check"
        current_position = "0"

    return total_cost


# Test Script
def test():
    test_cases = [
        {"input": "a", "expected": 2},
        {"input": "abc", "expected": 12},
        {"input": "az", "expected": 4},
        {"input": "jay", "expected": 26},
        {"input": "zaz", "expected": 6}, 
        {"input": "humanevalv", "expected": 138},
        {"input": "paperaccepted", "expected": 152},
        {"input": "abcdefghijklmnopqrstuvwxyz", "expected": 364},
        {"input": "davidtao", "expected": 86},
        {"input": "icmlaccepted", "expected": 152},
    ]

    for i, test_case in enumerate(test_cases, start=1):
        target = test_case["input"]
        expected_output = test_case["expected"]
        
        try:
            output = solution(target)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`target`: \"{target}\"\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

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

test()