public
Created

  • Download Gist
gistfile1.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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..]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.