Day 7 Part 2
This commit is contained in:
23
ressources/day07-input-test
Normal file
23
ressources/day07-input-test
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
|
||||||
17
src/Day7.hs
17
src/Day7.hs
@@ -81,7 +81,6 @@ filterDirectories predicate dir = filter (\(_, size) -> predicate size) $ sizeOf
|
|||||||
sumUp :: [(String, Int)] -> Int
|
sumUp :: [(String, Int)] -> Int
|
||||||
sumUp = sum . map snd
|
sumUp = sum . map snd
|
||||||
|
|
||||||
|
|
||||||
forceRight :: Either a b -> b
|
forceRight :: Either a b -> b
|
||||||
forceRight (Left _) = error "forced Right but got Left"
|
forceRight (Left _) = error "forced Right but got Left"
|
||||||
forceRight (Right b) = b
|
forceRight (Right b) = b
|
||||||
@@ -92,6 +91,16 @@ day7 = do
|
|||||||
putStrLn "Day7"
|
putStrLn "Day7"
|
||||||
let parsed = forceRight $ parseTerminalLines input
|
let parsed = forceRight $ parseTerminalLines input
|
||||||
let tree = buildTree parsed
|
let tree = buildTree parsed
|
||||||
let filtered = filterDirectories (<= 100000) tree
|
let small = filterDirectories (<= 100000) tree
|
||||||
let summed = sumUp filtered
|
let sumOfSmall = sumUp small
|
||||||
putStrLn ("Sum of those directories is " ++ show summed)
|
putStrLn ("Sum of those directories is " ++ show sumOfSmall)
|
||||||
|
let rootSize = snd . head $ sizeOfDirectories tree
|
||||||
|
putStrLn ("Root size is " ++ show rootSize)
|
||||||
|
let freeSpace = 70000000 - rootSize
|
||||||
|
let requiredSpace = 30000000 - freeSpace
|
||||||
|
putStrLn ("Required space is " ++ show requiredSpace)
|
||||||
|
let bigEnough = filterDirectories (>= requiredSpace) tree
|
||||||
|
let onlySizes = map snd bigEnough
|
||||||
|
let smallest = minimum onlySizes
|
||||||
|
putStrLn ("Smallest directory that is still large enough " ++ show smallest)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user