LeetCode 2257. Count Unguarded Cells in the Grid in F#

URL

Count Unguarded Cells in the Grid - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202411/count_unguarded_cells_in_the_grid/main.fsx

type State =
    | Unguarded
    | Guard
    | Wall
    | Guarded

let countUnguarded (m: int) (n: int) (guards: (int * int) list) (walls: (int * int) list) : int =
    let rec setGuarded (row, col) (x, y) (matrix: State[,]) =
        if row >= 0 && row < m && col >= 0 && col < n then
            match matrix.[row, col] with
            | Wall
            | Guard -> ()
            | _ ->
                matrix.[row, col] <- Guarded
                setGuarded (row + x, col + y) (x, y) matrix

    let matrix = Array2D.init m n (fun _ _ -> Unguarded)
    guards |> List.iter (fun (i, j) -> matrix.[i, j] <- Guard)
    walls |> List.iter (fun (i, j) -> matrix.[i, j] <- Wall)

    let directions = [ (-1, 0); (0, -1); (1, 0); (0, 1) ]

    guards
    |> List.iter (fun (row, col) ->
        directions
        |> List.iter (fun (x, y) -> setGuarded (row + x, col + y) (x, y) matrix))

    matrix |> Seq.cast<State> |> Seq.filter ((=) Unguarded) |> Seq.length