Created
November 20, 2017 23:00
-
-
Save lpenz/2f2c63fec6a449d17282de76afb64e84 to your computer and use it in GitHub Desktop.
minesweeper kata in haskell, no output
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
import qualified Data.Map as Map | |
import Control.Monad | |
main :: IO () | |
main = processOneField | |
processOneField :: IO () | |
processOneField = do | |
[n, m] <- (map read . words) <$> getLine :: IO [Int] | |
when ((n, m) /= (0, 0)) $ doProcessOneField n m | |
doProcessOneField :: Int -> Int -> IO () | |
doProcessOneField n m = do | |
arena <- replicateM n getLine | |
let coords = [ (i, j) | i <- [0..n-1], j <- [0..m-1] ] | |
let mines = filter ( \ c -> (arena !! fst c) !! snd c == '*' ) coords | |
let zeromap = Map.fromList $ map ( \ c -> (c, 0::Int) ) coords | |
print $ foldr (f coords) zeromap mines | |
processOneField | |
f :: [(Int, Int)] -> (Int, Int) -> Map.Map (Int, Int) Int -> Map.Map (Int, Int) Int | |
f coords (i, j) r = foldr (Map.adjust (+1)) r neighs | |
where | |
neighs = do | |
ii <- [i-1..i+1] | |
jj <- [j-1..j+1] | |
guard $ (ii, jj) `elem` coords | |
return (ii, jj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment