finish seven
This commit is contained in:
parent
09cab9fc54
commit
da4ae2a835
26 changed files with 2591 additions and 1419 deletions
42
app/seven.hs
42
app/seven.hs
|
@ -2,7 +2,6 @@
|
|||
|
||||
import Control.Monad
|
||||
|
||||
data FS = FS [Inode]
|
||||
data Inode = Dir Name [Inode] | File Name Size deriving(Show)
|
||||
type Name = String
|
||||
type Size = Int
|
||||
|
@ -60,26 +59,45 @@ initZipper = (Dir "/" [],[])
|
|||
|
||||
data InodeSize = DirS Size | FileS Size deriving(Show)
|
||||
|
||||
getSize (DirS s)= s
|
||||
getSize (DirS s )=s
|
||||
getSize (FileS s)=s
|
||||
|
||||
flattenToSmallDirs :: Inode -> [InodeSize]
|
||||
flattenToSmallDirs :: Inode -> (InodeSize,[InodeSize])
|
||||
flattenToSmallDirs (Dir _ children) =
|
||||
let childSizes =join $ map flattenToSmallDirs children
|
||||
currentSize=(sum $ map getSize $ childSizes) in
|
||||
(DirS currentSize):(filter isDir childSizes )
|
||||
flattenToSmallDirs (File _ size) = [FileS size]
|
||||
let childSizes =map flattenToSmallDirs children
|
||||
currentSize =(sum $ map getSize $ map fst childSizes) in
|
||||
(DirS currentSize ,filter isDir $ join $ map combineOwnWithChildSize childSizes )
|
||||
flattenToSmallDirs (File _ size) = (FileS size,[])
|
||||
|
||||
combineOwnWithChildSize :: (InodeSize,[InodeSize]) -> [InodeSize]
|
||||
combineOwnWithChildSize (own,children) = own:children
|
||||
|
||||
filterSmallDirs :: [InodeSize] -> [Size]
|
||||
filterSmallDirs ((DirS s):rest) = if (s<100000) then s:(filterSmallDirs rest) else filterSmallDirs rest
|
||||
filterSmallDirs ((FileS s):rest) = filterSmallDirs rest
|
||||
filterSmallDirs ((FileS _):rest) = filterSmallDirs rest
|
||||
filterSmallDirs [] = []
|
||||
|
||||
isDir (DirS s)=True
|
||||
isDir (FileS s)=False
|
||||
isDir :: InodeSize -> Bool
|
||||
isDir (DirS _)= True
|
||||
isDir (FileS _)=False
|
||||
|
||||
main = do
|
||||
mainP1 :: IO ()
|
||||
mainP1 = do
|
||||
content <- readFile "seven-input.txt"
|
||||
let statements = map parseEither $ drop 1 $ lines content
|
||||
let a = foldl foldFunction initZipper statements
|
||||
print $ sum $ filterSmallDirs $ flattenToSmallDirs $ fst $ fsUpToRoot a
|
||||
print $ sum $ filterSmallDirs $ combineOwnWithChildSize $ flattenToSmallDirs $ fst $ fsUpToRoot a
|
||||
|
||||
|
||||
mainP2::IO ()
|
||||
mainP2 = do
|
||||
content <- readFile "seven-input.txt"
|
||||
let statements = map parseEither $ drop 1 $ lines content
|
||||
a = foldl foldFunction initZipper statements
|
||||
(rootSize, dirSizes) =flattenToSmallDirs $ fst $ fsUpToRoot a
|
||||
currentlyFree=(70000000- getSize rootSize)
|
||||
requiredCleanup = 30000000-currentlyFree
|
||||
largeEnoughDirs = filter (\s -> s >= requiredCleanup) $ map getSize dirSizes
|
||||
print $ minimum largeEnoughDirs
|
||||
|
||||
main = mainP2
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue