Why is day seven so hard?

This commit is contained in:
qvalentin 2022-12-16 19:37:44 +01:00
parent 155f405189
commit 51dbc7a1fb
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
2 changed files with 57 additions and 0 deletions

23
seven-input.txt Normal file
View 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
View 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