Skip to content

@singpolyma /gist:3456629
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
import Data.Char
import qualified Data.Map as Map
main :: IO ()
main = mapM_ putStrLn =<<
(map (fmt . isAmbiguous . parse) . perms) `fmap` getContents
where
perms = skipN . init . lines
fmt True = "ambiguous"
fmt False = "unambiguous"
skipN :: [a] -> [a]
skipN (_:x:xs) = x : skipN xs
skipN [] = []
skipN _ = []
parse :: String -> [Int]
parse = map readIt . words
where
readIt = foldl (\n d -> n * 10 + fromIntegral (digitToInt d)) 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
Something went wrong with that request. Please try again.