module Part4 where
runLengthEncode :: Eq a => [a] -> [(a, Int)]
runLengthEncode [] = []
runLengthEncode (x:xs) = nextGroup x 1 xs
where
nextGroup e n [] = [(e, n)]
nextGroup e n (y:ys)
| e == y = nextGroup e (n + 1) ys
| otherwise = (e, n) : nextGroup y 1 ys
rlePropLengthPreserved :: [Int] -> Bool
rlePropLengthPreserved as = length as == (sum $ map snd $ runLengthEncode as)
rlePropDupesCollapsed :: Int -> Bool
rlePropDupesCollapsed n
| m == 0 = runLengthEncode "" == []
| otherwise = runLengthEncode (replicate m 'x') == [('x', m)]
where m = n `mod` 100
rlePropRoundTrip :: [Int] -> Bool
rlePropRoundTrip ns = runLengthEncode xs == is
where is = zip ['a'..] $ map (\n -> n `mod` 100 + 1) ns
xs = concatMap (\(i,n) -> replicate n i) is