advent-of-code/seven.hs

35 lines
886 B
Haskell
Raw Normal View History

2022-12-16 19:37:44 +01:00
data FS = FS [Inode]
data Inode = Dir Name [Inode] | File Name Size deriving(Show)
type Name = String
type Size = Int
data Command = Ls | Cd Name | CdUp | CdRoot deriving(Show)
data Line = LineC Command | LineI Inode deriving(Show)
parseCommand :: String -> Command
parseCommand ('c':'d':' ':name) =(Cd name)
parseCommand "ls" =Ls
parseCommand "cd .." =CdUp
parseCommand "cd /" =CdRoot
parseInode :: String -> Inode
parseInode ('d':'i':'r':' ': name) = Dir name []
parseInode input = File (drop 1 $ dropWhile (/= ' ') input) (read $ takeWhile (/= ' ') input)
parseEither :: String -> Line
parseEither ('$':' ':command) =LineC $ parseCommand command
parseEither input= LineI $ parseInode input
main = do
content <- readFile "seven-input.txt"
let statements = map parseEither $ lines content
print statements