# Problem Description
This is a terrain water flow optimization problem where:
- We have a series of terrain blocks of different heights
- Water can be poured at any block
- Water flows to adjacent blocks if they are at the same height or lower
- Water seeps into blocks (doesn't accumulate)
- Goal: Find the optimal pouring position (block index) that results in maximum water coverage area
- Water flow stops when it encounters a higher block or reaches the terrain edges

# Visual Facts
1. Each example shows a series of terrain blocks represented as bars
2. Heights are labeled with numbers (1-4)
3. Blue lines show water flow paths
4. Water flows both leftward and rightward from the pouring point
5. Example 1 terrain: [3,1,2,3,3,2,4]
6. Example 2 terrain: [3,1,4,3,3,2,4]
7. Both examples show water being poured at a block with height 3
8. Green text indicates the area "covered with water"

# Visual Patterns
1. Water Flow Rules:
   - Water flows horizontally to adjacent blocks
   - Flow continues if next block is same height or lower
   - Flow stops at higher blocks
   - Flow stops at terrain edges

2. Coverage Pattern:
   - Coverage area includes the pour point and all connected blocks that can be reached
   - A block is reachable if there's a path of same-or-lower height blocks from pour point
   - Higher blocks act as barriers, splitting the flow

3. Optimization Strategy:
   - Pour points at higher elevations can potentially cover more area
   - Ideal pour points are likely at local maxima
   - Central locations might be advantageous as they allow flow in both directions

4. Scoring Mechanism:
   - Coverage is counted by number of blocks water reaches
   - Each block counts as 1 unit regardless of height
   - Example 1 shows 5 blocks covered
   - Example 2 shows 3 blocks covered


# Solution
from typing import List

def solution(terrain: List[int]) -> int:
    """
    Determine the index of the terrain block where pouring water would result in the most area being covered with water. 
    The water will quickly seep into the terrain block and will not accumulate between the blocks.

    Input:
    - terrain: A list of integers representing a series of terrain blocks with differenct heights.

    Output:
    - An integer representing the zero-based index of the identified terrain block.
    """
    def calculate_covered_area(index: int) -> int:
        left = index
        while left > 0 and terrain[left - 1] <= terrain[left]:
            left -= 1
        right = index
        while right < len(terrain) - 1 and terrain[right + 1] <= terrain[right]:
            right += 1
        return right - left + 1
    max_area = 0
    best_index = 0
    for i in range(len(terrain)):
        current_area = calculate_covered_area(i)
        if current_area > max_area:
            max_area = current_area
            best_index = i
    return best_index


# Test Script
def test():
    test_cases = [
        ([3, 1, 2, 3, 3, 2, 4], [3, 4]),
        ([1, 2, 3, 4], 3),
        ([3, 2, 1, 1, 1], 0),
        ([1, 3, 3, 5, 3, 5, 9, 5, 1, 3, 6, 8, 6, 4, 2, 7, 9, 6, 4, 2, 8, 2, 3, 4, 5, 1, 7, 1], 11),
        ([1, 3, 3, 5, 9, 5, 1, 3, 6, 8, 6, 4, 2, 3, 4, 5, 1, 7, 1], 4),
        ([2, 5, 3, 6, 4], 1),
        ([8, 8, 5, 5, 3, 3, 7, 7, 5, 5, 6, 6, 3, 3, 8, 8, 6, 6, 3, 3, 1, 1, 3, 3, 4, 4, 7, 7, 9, 9], 14)
    ]

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

        if isinstance(expected_output, list):
            if output not in expected_output:
                error_message = (
                    f"The following Test Case failed:\n"
                    f"`terrain`: {terrain}\n\n"
                    f"Actual Output: {output}\n"
                    f"Expected Output should be one of: {expected_output}"
                )
                assert False, error_message
        else:
            if output != expected_output:
                error_message = (
                    f"The following Test Case failed:\n"
                    f"`terrain`: {terrain}\n\n"
                    f"Actual Output: {output}\n"
                    f"Expected Output: {expected_output}"
                )
                assert False, error_message

test()