Created
April 16, 2015 03:58
-
-
Save pscollins/4c66d03ca5e8411928ca to your computer and use it in GitHub Desktop.
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 Control.Monad.ST | |
import Control.Monad | |
import Data.Array | |
import Data.Array.ST | |
import Data.List (elemIndex) | |
import Data.Maybe (fromMaybe) | |
newSTArray :: (Ix i) => (i,i) -> e -> ST s (STArray s i e) | |
newSTArray = newArray | |
minFree :: [Int] -> Int | |
minFree ns = fromMaybe 0 $ elemIndex True seen | |
where | |
bnd = (0, length ns) | |
safeWrite arr n = when (inRange bnd n) $ writeArray arr n False | |
setTrues arr = mapM_ (safeWrite arr) ns >> return arr | |
seen = elems $ runSTArray $ newSTArray bnd False >>= setTrues | |
main :: IO () | |
main = interact id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment