diff --git a/07/Main.idr b/07/Main.idr index 9818483..f07cfe1 100644 --- a/07/Main.idr +++ b/07/Main.idr @@ -1,4 +1,5 @@ import Data.List +import Data.List1 import Data.Vect import Data.String import System.File.ReadWrite @@ -191,8 +192,6 @@ applyCommands xs x = helper (Stk []) xs x do result <- foldlM (insertEntry (current stack)) node entries helper stack ys result - - part1 : String -> IO () part1 input = case parseCmds (lines input) of @@ -204,9 +203,37 @@ part1 input = let wanted = sum . filter (<= 100000) . map size . directories $ node in putStrLn $ "Part 1: " ++ show wanted +minBy : Ord b => (a -> b) -> a -> a -> a +minBy f x y = + let i = f x + j = f y + in if i < j then x else y + +part2 : String -> IO () +part2 input = + case parseCmds (lines input) of + Nothing => putStrLn "Failed to parse commands" + (Just commands) => + case applyCommands commands emptyNode of + Nothing => putStrLn "Failed to apply commands" + (Just node) => + let totalSpace = 70000000 + neededSpace = 30000000 + freeSpace = minus totalSpace (size node) + deficit = minus neededSpace freeSpace + canidates = filter ((>= deficit) . size) (directories node) + in case Data.List1.fromList canidates of + Nothing => putStrLn "Only one canidate?" + (Just canidates) => + let smallest = foldl1 (minBy size) canidates + in putStrLn $ "Part 2: " ++ (show (size smallest)) + + partial main : IO () main = do file <- readFile "input" case file of - (Right contents) => part1 contents + (Right contents) => + do part1 contents + part2 contents (Left err) => printLn err