Skip to content

Instantly share code, notes, and snippets.

@pscollins
Created April 16, 2015 03:58
Show Gist options
  • Save pscollins/4c66d03ca5e8411928ca to your computer and use it in GitHub Desktop.
Save pscollins/4c66d03ca5e8411928ca to your computer and use it in GitHub Desktop.
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