Day nine
This commit is contained in:
31
day-09-haskell/src/Common.hs
Normal file
31
day-09-haskell/src/Common.hs
Normal file
@@ -0,0 +1,31 @@
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user