Skip to content

Instantly share code, notes, and snippets.

@olefriis
Created May 23, 2012 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olefriis/2776707 to your computer and use it in GitHub Desktop.
Save olefriis/2776707 to your computer and use it in GitHub Desktop.
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