Jens Kadenbach преди 2 години
родител
ревизия
33871a7f64
променени са 2 файла, в които са добавени 36 реда и са изтрити 4 реда
  1. 23
    0
      ressources/day07-input-test
  2. 13
    4
      src/Day7.hs

+ 23
- 0
ressources/day07-input-test Целия файл

@@ -0,0 +1,23 @@
1
+$ cd /
2
+$ ls
3
+dir a
4
+14848514 b.txt
5
+8504156 c.dat
6
+dir d
7
+$ cd a
8
+$ ls
9
+dir e
10
+29116 f
11
+2557 g
12
+62596 h.lst
13
+$ cd e
14
+$ ls
15
+584 i
16
+$ cd ..
17
+$ cd ..
18
+$ cd d
19
+$ ls
20
+4060174 j
21
+8033020 d.log
22
+5626152 d.ext
23
+7214296 k

+ 13
- 4
src/Day7.hs Целия файл

@@ -81,7 +81,6 @@ filterDirectories predicate dir = filter (\(_, size) -> predicate size) $ sizeOf
81 81
 sumUp :: [(String, Int)] -> Int
82 82
 sumUp = sum . map snd
83 83
 
84
-
85 84
 forceRight :: Either a b -> b
86 85
 forceRight (Left _) = error "forced Right but got Left"
87 86
 forceRight (Right b) = b
@@ -92,6 +91,16 @@ day7 = do
92 91
    putStrLn "Day7"
93 92
    let parsed = forceRight $ parseTerminalLines input
94 93
    let tree = buildTree parsed
95
-   let filtered = filterDirectories (<= 100000) tree
96
-   let summed = sumUp filtered
97
-   putStrLn ("Sum of those directories is " ++ show summed)
94
+   let small = filterDirectories (<= 100000) tree
95
+   let sumOfSmall = sumUp small
96
+   putStrLn ("Sum of those directories is " ++ show sumOfSmall)
97
+   let rootSize = snd . head $ sizeOfDirectories tree
98
+   putStrLn ("Root size is " ++ show rootSize)
99
+   let freeSpace = 70000000 - rootSize
100
+   let requiredSpace = 30000000 - freeSpace
101
+   putStrLn ("Required space is " ++ show requiredSpace)
102
+   let bigEnough = filterDirectories (>= requiredSpace) tree
103
+   let onlySizes = map snd bigEnough
104
+   let smallest = minimum onlySizes
105
+   putStrLn ("Smallest directory that is still large enough " ++ show smallest)
106
+

Loading…
Отказ
Запис