Available Captures for Rook

I tried to simplify the solution for this easy LC problem but I guess this could’ve been the simplest I can get to. Not a beautiful code but this’ll do for now.

class Solution {
    fun numRookCaptures(board: Array<CharArray>): Int {
        var rookPos: Pair<Int, Int>? = null
        
        for(index in 0 until board.size){
            for(subIndex in 0 until board.size){
                if(board[subIndex][index] == 'R'){
                    rookPos = Pair(subIndex, index)
                    break
                }else if(board[index][subIndex] == 'R'){
                    rookPos = Pair(index, subIndex)
                    break
                }                
            }
            
            if(rookPos != null) break
        }
        
        val currentX = rookPos!!.second
        val currentY = rookPos!!.first

        var directions = intArrayOf(currentX - 1, currentY - 1, currentX + 1, currentY + 1)        
        var state = booleanArrayOf(true, true, true, true)

        var pCount = 0
        
        while(state[0] || state[1] || state[2] || state[3]){
            
            (0..1).forEach { if(directions[it] < 0) state[it] = false }
            (2..3).forEach { if(directions[it] > board.size - 1) state[it] = false }
                        
            // check left
            pCount += check(state, 0, board, directions[0], currentY, directions, 0, false)
            
            // check top
            pCount += check(state, 1, board, currentX, directions[1], directions, 1, false)
            
            // check right
            pCount += check(state, 2, board, directions[2], currentY, directions, 2, true)
            
            // check down
            pCount += check(state, 3, board, currentX, directions[3], directions, 3, true)
        
        }
        
        return pCount
    }
    
    fun check(state: BooleanArray, stateIndex: Int,
                       board: Array<CharArray>,
                      boardX: Int, 
                      boardY: Int, direction: IntArray, 
                       directionIndex: Int, increment: Boolean) : Int{
        var output = 0
        
        if(state[stateIndex]) {
                if(board[boardY][boardX] == 'p'){                    
                    state[stateIndex] = false
                    output++
                }else if(board[boardY][boardX] == 'B'){
                    state[stateIndex] = false
                }else{
                    if(increment) direction[directionIndex]++ else direction[directionIndex]--
                }
            }
        
        return output
    }
}

The first two loops will try to find the location of our Rook. Since this is an N*N matrix, we can simultaneously find our Rook from both horizontal and vertical directions. Once we found our Rook, then we simultaneously traverse in all directions and look for pawns. If we hit a bishop, a pawn or reached the edge of the board then we stop finding for that particular direction. Everytime we hit a pawn, we increment a counter which will be our final output.

Runtime: 128 ms, faster than 90.91% of Kotlin online submissions for Available Captures for Rook.
Memory Usage: 31.7 MB, less than 100.00% of Kotlin online submissions for Available Captures for Rook.