Untitled

 avatar
unknown
plain_text
12 days ago
683 B
3
Indexable
fun solution(R: Array<String>): Int {
    val (rows, cols) = R.size to R[0].length
    val track = Array(rows) { Array(cols) { BooleanArray(4) } }
    
    var (r, c, dir) = Triple(0, 0, 0)
    val moves = listOf(0 to 1, 1 to 0, 0 to -1, -1 to 0)
    
    while (true) {
        if (track[r][c][dir]) break
        track[r][c][dir] = true
        
        val (dr, dc) = moves[dir]
        val (nr, nc) = r + dr to c + dc
        
        if (nr in 0 until rows && nc in 0 until cols && R[nr][nc] == '.') {
            r = nr
            c = nc
        } else {
            dir = (dir + 1) % 4
        }
    }
    
    return track.sumOf { row -> row.count { it.any { v -> v } } }
}
Leave a Comment