module Solutions.P28 (lsort, lfsort) where
import Data.List (sortOn)
import Data.Map.Lazy ((!))
import qualified Data.Map.Lazy as Map
lsort :: [[a]] -> [[a]]
lsort :: forall a. [[a]] -> [[a]]
lsort = ([a] -> Int) -> [[a]] -> [[a]]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
lfsort :: [[a]] -> [[a]]
lfsort :: forall a. [[a]] -> [[a]]
lfsort [[a]]
xs = ([a] -> Int) -> [[a]] -> [[a]]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (\[a]
ys -> Map Int Int
fs Map Int Int -> Int -> Int
forall k a. Ord k => Map k a -> k -> a
! [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ys) [[a]]
xs
where fs :: Map Int Int
fs = (Int -> Int -> Int) -> [(Int, Int)] -> Map Int Int
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) ([(Int, Int)] -> Map Int Int) -> [(Int, Int)] -> Map Int Int
forall a b. (a -> b) -> a -> b
$ ([a] -> (Int, Int)) -> [[a]] -> [(Int, Int)]
forall a b. (a -> b) -> [a] -> [b]
map (\[a]
ys -> ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ys, Int
1 :: Int)) [[a]]
xs