module Exercise05 where 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 = let mult = 2 ^ length ds in if null ds || minimum ds < 1 then [] else snd $ mapAccumR (\prev this -> let prod = product this in (prod, prod - mult * prev)) 0 $ take (log2 (minimum ds) + 1) $ iterate (map (`div` 2)) ds {- (pretty much) the same just less cramped, I don't think the preformance is affected by calculating minimum ds twice, so I went with the one line version decompose ds = let mult = 2 ^ length ds lowest = minimum ds in if null ds || lowest < 1 then [] else snd $ mapAccumR (\prev this -> let prod = product this in (prod, prod - mult * prev)) 0 $ take (log2 lowest + 1) $ iterate (map (`div` 2)) ds -} {-TTEW-} test :: Bool test = decompose [123] == [1 ,1 ,0 ,1 ,1 ,1 ,1] && decompose [6, 5] == [6, 2, 1] && decompose [9, 6] == [6, 4, 2] && decompose [8, 4, 4] == [0, 0, 2] && decompose [10 , 10, 10] == [0, 61, 0, 1] && decompose [10 , 5, 5] == [90 , 4, 2] && decompose [6, 5, 4] == [24 , 4, 1] && decompose [9, 18, 27, 36] == [22680 , 1512 , 48, 24] && null (decompose []) && null (decompose [0]) && null (decompose [0,9,12]) time :: Int -> Int time a = length . show . sum . decompose $ replicate (10^4) (10^a) time10 :: Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -- time10 12 12 12 12 12 12 12 12 12 12 -- time10 15 15 15 15 15 15 15 15 15 15 time10 a b c d e f g h i j = (length . show . sum . decompose $ replicate (10^4) (10^a)) + (length . show . sum . decompose $ replicate (10^4) (10^b)) + (length . show . sum . decompose $ replicate (10^4) (10^c)) + (length . show . sum . decompose $ replicate (10^4) (10^d)) + (length . show . sum . decompose $ replicate (10^4) (10^e)) + (length . show . sum . decompose $ replicate (10^4) (10^f)) + (length . show . sum . decompose $ replicate (10^4) (10^g)) + (length . show . sum . decompose $ replicate (10^4) (10^h)) + (length . show . sum . decompose $ replicate (10^4) (10^i)) + (length . show . sum . decompose $ replicate (10^4) (10^j))