Created
May 23, 2012 18:01
-
-
Save olefriis/2776707 to your computer and use it in GitHub Desktop.
Programming with the Stars: Mark Seeman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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