module Solutions.P13 (encodeDirect) where
import Problems.Lists
encodeDirect :: Eq a => [a] -> [Encoding a]
encodeDirect :: forall a. Eq a => [a] -> [Encoding a]
encodeDirect [] = []
encodeDirect (a
x:[a]
xs) = Int -> a -> Encoding a
forall a. Int -> a -> Encoding a
encode (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) a
x Encoding a -> [Encoding a] -> [Encoding a]
forall a. a -> [a] -> [a]
: [a] -> [Encoding a]
forall a. Eq a => [a] -> [Encoding a]
encodeDirect [a]
r
where (Int
n, [a]
r) = a -> [a] -> (Int, [a])
forall a. Eq a => a -> [a] -> (Int, [a])
consume a
x [a]
xs
consume :: Eq a => a -> [a] -> (Int, [a])
consume :: forall a. Eq a => a -> [a] -> (Int, [a])
consume a
_ [] = (Int
0, [])
consume a
x l :: [a]
l@(a
y:[a]
ys)
| a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = let (Int
n, [a]
zs) = a -> [a] -> (Int, [a])
forall a. Eq a => a -> [a] -> (Int, [a])
consume a
x [a]
ys in (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1, [a]
zs)
| Bool
otherwise = (Int
0, [a]
l)
encode :: Int -> a -> Encoding a
encode :: forall a. Int -> a -> Encoding a
encode Int
1 a
x = a -> Encoding a
forall a. a -> Encoding a
Single a
x
encode Int
n a
x = Int -> a -> Encoding a
forall a. Int -> a -> Encoding a
Multiple Int
n a
x