hmmm
This commit is contained in:
parent
02b62f919b
commit
b4ad4cffd8
22
seven.hs
22
seven.hs
|
@ -1,5 +1,6 @@
|
||||||
-- everything stolen from http://learnyouahaskell.com/zippers#a-very-simple-file-system
|
-- everything stolen from http://learnyouahaskell.com/zippers#a-very-simple-file-system
|
||||||
|
|
||||||
|
import Control.Monad
|
||||||
|
|
||||||
data FS = FS [Inode]
|
data FS = FS [Inode]
|
||||||
data Inode = Dir Name [Inode] | File Name Size deriving(Show)
|
data Inode = Dir Name [Inode] | File Name Size deriving(Show)
|
||||||
|
@ -57,8 +58,27 @@ initZipper :: FSZipper
|
||||||
initZipper = (Dir "/" [],[])
|
initZipper = (Dir "/" [],[])
|
||||||
|
|
||||||
|
|
||||||
|
data InodeSize = DirS Size | FileS Size deriving(Show)
|
||||||
|
|
||||||
|
getSize (DirS s)= s
|
||||||
|
getSize (FileS s)=s
|
||||||
|
|
||||||
|
flattenToSmallDirs :: Inode -> [InodeSize]
|
||||||
|
flattenToSmallDirs (Dir _ children) =
|
||||||
|
let childSizes =join $ map flattenToSmallDirs children
|
||||||
|
currentSize=(sum $ map getSize $ childSizes) in
|
||||||
|
if (10 < 100000) then (DirS currentSize):childSizes else childSizes
|
||||||
|
flattenToSmallDirs (File _ size) = [FileS size]
|
||||||
|
|
||||||
|
filterSmallDirs :: [InodeSize] -> [Size]
|
||||||
|
filterSmallDirs ((DirS s):rest) = if (s<100000) then s:(filterSmallDirs rest) else filterSmallDirs rest
|
||||||
|
filterSmallDirs ((FileS s):rest) = filterSmallDirs rest
|
||||||
|
filterSmallDirs [] = []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
content <- readFile "seven-input.txt"
|
content <- readFile "seven-input.txt"
|
||||||
let statements = map parseEither $ drop 1 $ lines content
|
let statements = map parseEither $ drop 1 $ lines content
|
||||||
let a = foldl foldFunction initZipper statements
|
let a = foldl foldFunction initZipper statements
|
||||||
print $ fsUpToRoot a
|
print $sum$ filterSmallDirs $ flattenToSmallDirs $ fst $ fsUpToRoot a
|
||||||
|
|
Loading…
Reference in a new issue