Skip to content

Instantly share code, notes, and snippets.

@singpolyma
Created August 24, 2012 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singpolyma/3456868 to your computer and use it in GitHub Desktop.
Save singpolyma/3456868 to your computer and use it in GitHub Desktop.
import Data.Char (ord)
import qualified Data.Map as Map
import qualified Data.ByteString as BS
main :: IO ()
main = BS.getContents >>=
mapM_ (putStrLn . fmt . isAmbiguous . parse) . skipN . BS.split nl
where
nl = fromIntegral $ ord '\n'
fmt True = "ambiguous"
fmt False = "unambiguous"
skipN :: [a] -> [a]
skipN (_:x:xs) = x : skipN xs
skipN [] = []
skipN _ = []
parse :: BS.ByteString -> [Int]
parse = map readIt . BS.split (fromIntegral $ ord ' ')
where
readIt = BS.foldl (\n d -> n * 10 + (fromIntegral d - ord '0')) 0
isAmbiguous :: [Int] -> Bool
isAmbiguous = snd . foldl (\(m,a) (i,v) ->
case i `compare` v of
EQ -> (m, a)
LT -> (Map.insert i v m, a)
GT -> (m, a && Just i == Map.lookup v m)
) (Map.empty, True) . zip [1..]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment