Day 7 Part 2
This commit is contained in:
parent
5d0e711569
commit
1e25a65c09
33
07/Main.idr
33
07/Main.idr
|
@ -1,4 +1,5 @@
|
||||||
import Data.List
|
import Data.List
|
||||||
|
import Data.List1
|
||||||
import Data.Vect
|
import Data.Vect
|
||||||
import Data.String
|
import Data.String
|
||||||
import System.File.ReadWrite
|
import System.File.ReadWrite
|
||||||
|
@ -191,8 +192,6 @@ applyCommands xs x = helper (Stk []) xs x
|
||||||
do result <- foldlM (insertEntry (current stack)) node entries
|
do result <- foldlM (insertEntry (current stack)) node entries
|
||||||
helper stack ys result
|
helper stack ys result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
part1 : String -> IO ()
|
part1 : String -> IO ()
|
||||||
part1 input =
|
part1 input =
|
||||||
case parseCmds (lines input) of
|
case parseCmds (lines input) of
|
||||||
|
@ -204,9 +203,37 @@ part1 input =
|
||||||
let wanted = sum . filter (<= 100000) . map size . directories $ node
|
let wanted = sum . filter (<= 100000) . map size . directories $ node
|
||||||
in putStrLn $ "Part 1: " ++ show wanted
|
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 ()
|
partial main : IO ()
|
||||||
main =
|
main =
|
||||||
do file <- readFile "input"
|
do file <- readFile "input"
|
||||||
case file of
|
case file of
|
||||||
(Right contents) => part1 contents
|
(Right contents) =>
|
||||||
|
do part1 contents
|
||||||
|
part2 contents
|
||||||
(Left err) => printLn err
|
(Left err) => printLn err
|
||||||
|
|
Loading…
Reference in New Issue