Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let input = "4 4
- *...
- ....
- .*..
- ...."
- let parseLines (lines:string[]) = [
- let i = ref 0
- while !i < lines.Length do
- let header = lines.[!i]
- let n, m =
- match header.Split(' ') with
- | [|n;m|] -> int n, int m
- | _ -> failwith "Failed to parse header"
- yield n,m,
- [for y = 1 to n do
- let line = lines.[!i+y]
- yield [for c in line -> c]
- ]
- i := !i + n + 1
- ]
- let parse (s:string) =
- let options = System.StringSplitOptions.RemoveEmptyEntries
- let lines = s.Split([|'\r';'\n'|], options)
- parseLines lines
- let value c =
- match c with
- | '*' -> 1
- | '.' -> 0
- | _ -> failwith "Unexpected value"
- let compute n m (board:char list list) =
- let count (x,y) =
- [-1,-1; 0,-1; 1,-1
- -1, 0; 1, 0
- -1, 1; 0, 1; 1, 1]
- |> List.sumBy (fun (dx,dy) ->
- let x, y = x + dx, y + dy
- if y>=0 && y<n && x>=0 && x<m
- then board.[y].[x] |> value
- else 0
- )
- board |> List.mapi (fun y line ->
- line |> List.mapi (fun x c ->
- match c with
- | '*' -> c
- | '.' -> '0' + char (count(x,y))
- | _ -> failwith "Unexpected value"
- )
- )
- let boards = parse input
- let n,m,board = boards |> List.head
- let result = compute n m board
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement