Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2012 11:34
Show Gist options
  • Save anonymous/4336084 to your computer and use it in GitHub Desktop.
Save anonymous/4336084 to your computer and use it in GitHub Desktop.
import Data.SBV
import Data.List.Split
import Control.Applicative
import Control.Monad
import Data.Char
(!!?) :: [SBool] -> Int -> SBool
ls !!? j
| j < 0 = false
| j >= length ls = false
| otherwise = ls !! j
(!!??) :: [[SBool]] -> Int -> [SBool]
ls !!?? j
| j < 0 = []
| j >= length ls = []
| otherwise = ls !! j
main :: IO ()
main = do
t <- map read . words <$> getLine
case t of
[0, 0] -> do
return ()
[h, w] -> do
bd <- replicateM h $ getLine
go h w bd
main
go :: Int -> Int -> [[Char]] -> IO ()
go h w bd = do
let count [] = 0 :: SInteger
count (x:xs) = (ite x 1 0) + count xs
mine ass = do
let dotIsNotMine =
[ bnot a
| j <- [0..h-1]
, i <- [0..w-1]
, let c = bd !! j !! i
, let a = ass !! j !! i
, c == '.'
]
numIsCorrect =
[ sum
[ ite a 1 0
| dj <- [-1 .. 1]
, di <- [-1 .. 1]
, let a = ass !!?? (j + dj) !!? (i + di)
]
.== (literal n :: SInteger)
| j <- [0..h-1]
, i <- [0..w-1]
, let c = bd !! j !! i
, isDigit c
, let n = read [c] :: Integer
]
bAnd $ dotIsNotMine ++ numIsCorrect
res <- minimize (Iterative True) count (h * w) (mine . chunksOf w)
print res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment