# Problem Description
This is a matrix traversal problem where we need to:
- Create a 4n × 4n matrix filled with consecutive integers starting from 1 to (4n)²
- Follow a specific red path pattern starting from a calculated position
- Return the sequence of numbers encountered along this path
- The path always ends at position (4n)² (bottom-right corner)

# Visual Facts
1. Matrix Structure:
   - N=1: 4×4 matrix with numbers 1-16
   - N=2: 8×8 matrix with numbers 1-64
   - Numbers arranged sequentially in row-major order

2. Path Examples:
   - N=1 path: [10,6,2,3,4,8,12,16]
   - N=2 path: [36,28,20,21,22,30,38,46,54,53,52,51,50,42,34,26,18,10,2,3,4,5,6,7,8,16,24,32,40,48,56,64]

3. Starting Positions:
   - N=1 starts at 10 (row 2, col 1) (0-indexed)
   - N=2 starts at 36 (row 4, col 3) (0-indexed)


# Visual Patterns
1. Matrix Size Rule:
   - Size = 4n × 4n
   - Total elements = (4n)²

2. Starting Position Pattern:
   - Start row = 4n/2  (0-indexed)
   - Start column = (4n/2)-1 (0-indexed)

3. Path Movement Rules:
   - Follows a clockwise spiral pattern
   - Movement alternates between:
     * up, right, down, left, up, right, down, left, ...
   - Initial step size = 2
     * Step size increases by 2 every two border encounters
     * The step size for the first moving up and then moving right is 2
     * The step size for the next moving down and then moving left is 4
     * The step size for the next moving up and then moving right is 6
     * The step size for the next moving down and then moving left is 8
     * And so on...
   - Every time after path moving left or right, the path setp increases by 2.
     e.g. starting from (2n,2n-1), the path visit: (2n,2n-1)->(2n-2,2n-1) (moving up 2 steps), (2n-2,2n-1)->(2n-2,2n+1) (moving right 2 steps), (2n-2,2n+1)->(2n+2,2n+1)(moving down 4 steps) …… repeat until it moving inward to (4n-1,4n-1)
     The path moves 1 step every time, so all the number visited on its way should be outputed.
   - The path will stop at (4n-1,4n-1), the path should not get out of matrix

4. Termination:
   - Always ends at position (4n)² (bottom-right corner)
   - Path gradually works its way to the outside of the matrix before moving inward to position (4n)²

# Solution
from typing import List

def solution(n: int) -> List[int]:
    """
    Traverses a 4n x 4n matrix following a specified red path and returns the values encountered.

    Parameters:
        n (int): Integer that determines the matrix dimensions (4n x 4n).

    Returns:
        List[int]: List of values encountered while traversing the red path.
    """
    mat = lambda i, j : 4 * n * i + j + 1
    answer = []
            
    def getAns(i,j, direction, ans):
        ans.append(mat(i,j))
        step = 2
        while 0 <= i < 4*n and 0 <= j < 4*n:
            temp = 1
            while temp <= step:
                # 0->up, 1-> right, 2-> down, 3-> left
                if direction == 0:
                    i = i - 1
                elif direction == 1:
                    j = j + 1
                elif direction == 2:
                    i = i + 1
                else:
                    j = j - 1
                ans.append(mat(i,j))   
                temp += 1
            direction = (direction + 1)%4
            if direction == 0 or direction == 2:
                step += 2
    
    direction = 0
    i = 2 * n
    j = 2 * n - 1
    getAns(i, j, direction, answer)
    answer.pop()
    
    return answer


# Test Script
def reference(n):
    mat = lambda i, j : 4 * n * i + j + 1
    answer = []
            
    def getAns(i,j, direction, ans):
        ans.append(mat(i,j))
        step = 2
        while 0 <= i < 4*n and 0 <= j < 4*n:
            temp = 1
            while temp <= step:
                # 0->up, 1-> right, 2-> down, 3-> left
                if direction == 0:
                    i = i - 1
                elif direction == 1:
                    j = j + 1
                elif direction == 2:
                    i = i + 1
                else:
                    j = j - 1
                ans.append(mat(i,j))   
                temp += 1
            direction = (direction + 1)%4
            if direction == 0 or direction == 2:
                step += 2
    
    direction = 0
    i = 2 * n
    j = 2 * n - 1
    getAns(i, j, direction, answer)
    answer.pop()
    
    return answer

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

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

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

test()