finish seven

This commit is contained in:
qvalentin 2022-12-17 18:00:11 +01:00
parent 09cab9fc54
commit da4ae2a835
Signed by: qvalentin
GPG key ID: C979FA1EAFCABF1C
26 changed files with 2591 additions and 1419 deletions

View file

@ -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