Ad
Games
Arrays
Data Types
Algorithms
Logic

Very readable solution (though not tail-recursive).
Could be used as spec for quick check based tests in case a more performant solution is required.

Code
Diff
  • module Platforms where
    
    platforms :: [Integer] -> Integer
    platforms (x:y:xs) = abs (x - y) + platforms (y:xs)
    platforms _        = 0
    • module Platforms where
    • platforms :: [Integer] -> Integer
    • platforms xs = sum $ zipWith energy xs (tail xs)
    • where
    • energy x y = abs (x - y)
    • platforms (x:y:xs) = abs (x - y) + platforms (y:xs)
    • platforms _ = 0
Games
Arrays
Data Types
Algorithms
Logic

Made algorithm more readable

Code
Diff
  • module Platforms where
    
    platforms :: [Integer] -> Integer
    platforms xs = sum $ zipWith energy xs (tail xs)
      where
      energy x y = abs (x - y)
    • module Platforms where
    • platforms :: [Integer] -> Integer
    • platforms [] = 0
    • platforms (_:[]) = 0
    • platforms (x:xs) = platforms' (abs . (x -), xs) where
    • platforms' :: (Integer -> Integer, [Integer]) -> Integer
    • platforms' (acc, x:[]) = acc x
    • platforms' (acc, x:xs) = platforms' (((+) (acc x)) . abs . (x -), xs)
    • platforms xs = sum $ zipWith energy xs (tail xs)
    • where
    • energy x y = abs (x - y)