Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Programming with the Stars: Mark Seeman
let input = "**..
....
.*..
...."
let compute (board : string) =
let board = board.Split([| "\n" |], System.StringSplitOptions.RemoveEmptyEntries)
let count (x, y) =
[-1, -1; 0, -1; 1, -1;
-1, 0; 1, 0;
-1, 1; 0, 1; 1, 1]
|> List.map (fun (x', y') -> x + x', y + y')
|> List.filter (fun (x, y) -> x >= 0 && x < board.[0].Length && y >= 0 && y < board.Length)
|> List.sumBy (fun (x, y) ->
if board.[y].[x] = '*'
then 1
else 0
)
board
|> Array.mapi (fun y line ->
line.ToCharArray() |> Array.mapi (fun x c ->
match c with
| '*' -> '*'
| '.' -> '0' + char (count(x, y))
| _ -> invalidArg "c" "Boo hiss!"
)
)
|> Array.map (fun chars -> System.String(chars))
|> Array.reduce (sprintf "%s\n%s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.