@LearnWithKiritiKodee Practice · LeetCode 54

Kodee's SnakeBot: Spiral Matrix

Next turnDOWN ↓ at 4
Finish at12

YOU arrow keys / WASD

cells 1/12
dir: RIGHT →
output: [1]

SNAKEBOT Kodee's code plays itself

cells 1/12
dir: RIGHT →for col = left .. right
output: [1]
Controls

Turn clockwise: right, down, left, up, and again. After every side, that wall moves one step in. That is all SnakeBot knows.

SnakeBot's brain: the four walls (optimal, O(m·n) time, O(1) extra space)
top = 0, bottom = rows - 1, left = 0, right = cols - 1
while (top <= bottom && left <= right) {
    for col = left .. right:   take matrix[top][col]      // →
    top++
    for row = top .. bottom:   take matrix[row][right]    // ↓
    right--
    if (top <= bottom)
        for col = right .. left: take matrix[bottom][col] // ←
        bottom--
    if (left <= right)
        for row = bottom .. top: take matrix[row][left]   // ↑
        left++
}