module Exercise05 where import Data.Bits import Data.List -- May or may not be useful: computes the logarithm base 2 (rounded down) of the given number. -- You don't have to move this into the WETT tags if you want to use it. log2 :: (Integral a, Num b) => a -> b log2 = let go acc n = if n <= 1 then acc else go (acc + 1) (n `div` 2) in go 0 {-WETT-} decompose :: [Integer] -> [Integer] decompose ds = if lengthC <= 0 then [] else snd $mapAccumR (\consumed position -> let count = countAt position in (count * scaleFactor, count - consumed)) 0 [0 .. log2 lengthC] where lengthC = minimum ds scaleFactor = 2 ^ length ds dsGrouped = map (\x -> (head x, genericLength x)) $group $sortOn (* (-1)) ds -- Sort descending for more smaller multiplications and group to save unnecessary calculations countAt position = foldr (\(lengthDim, times) acc -> shiftR lengthDim position ^ times * acc) 1 dsGrouped -- shiftR by position = div by 2^position {-TTEW-}