-- http://www.codewars.com/kata/53e57dada0cb0400ba000688
module AlphabeticAnagrams where
import Data.List

fact :: Int -> Integer
fact n = product [1 .. toInteger n]

multiperms xs = fact (sum ys) `div` product (map fact ys) where
  ys = (map length . group . sort) xs

lexiPos [x] = 1
lexiPos xs'@(x:xs) = sum (map multiperms ys) + lexiPos xs where
  ys = nub $ map (sort . tail) $ filter (\(z:_) -> z < x) $ zipWith (\i t -> head t : i ++ tail t ) (init $ tail $ inits xs') (tail $ init $ tails xs')
