32 lines
940 B
Haskell
32 lines
940 B
Haskell
module Common where
|
|
|
|
data BlockType = Empty | Allocated Int
|
|
|
|
instance Show BlockType where
|
|
show Empty = "(empty)"
|
|
show (Allocated blockId) = show blockId
|
|
instance Eq BlockType where
|
|
(==) Empty Empty = True
|
|
(==) (Allocated a) (Allocated b) = True
|
|
(==) _ _ = False
|
|
|
|
type Block = (BlockType, Int)
|
|
length :: Block -> Int
|
|
length (_, l) = l
|
|
|
|
parseBlocks :: String -> [Block]
|
|
parseBlocks = startWithBlock 0
|
|
where
|
|
startWithBlock :: Int -> String -> [Block]
|
|
startWithBlock _ [] = []
|
|
startWithBlock blockId (size:rest)
|
|
| size >= '0' && size <= '9' = (Allocated blockId, read [size]) : startWithEmpty (blockId + 1) rest
|
|
| otherwise = startWithBlock blockId rest
|
|
|
|
startWithEmpty :: Int -> String -> [Block]
|
|
startWithEmpty _ [] = []
|
|
startWithEmpty blockId (size:rest)
|
|
| size >= '0' && size <= '9' = (Empty, read [size]) : startWithBlock blockId rest
|
|
| otherwise = startWithEmpty blockId rest
|
|
|