Why is day seven so hard?
This commit is contained in:
parent
155f405189
commit
51dbc7a1fb
23
seven-input.txt
Normal file
23
seven-input.txt
Normal file
|
@ -0,0 +1,23 @@
|
|||
$ cd /
|
||||
$ ls
|
||||
dir a
|
||||
14848514 b.txt
|
||||
8504156 c.dat
|
||||
dir d
|
||||
$ cd a
|
||||
$ ls
|
||||
dir e
|
||||
29116 f
|
||||
2557 g
|
||||
62596 h.lst
|
||||
$ cd e
|
||||
$ ls
|
||||
584 i
|
||||
$ cd ..
|
||||
$ cd ..
|
||||
$ cd d
|
||||
$ ls
|
||||
4060174 j
|
||||
8033020 d.log
|
||||
5626152 d.ext
|
||||
7214296 k
|
34
seven.hs
Normal file
34
seven.hs
Normal file
|
@ -0,0 +1,34 @@
|
|||
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
|
Loading…
Reference in a new issue