summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Grois <andi@grois.info>2022-12-18 20:36:07 +0100
committerAndreas Grois <andi@grois.info>2022-12-18 20:36:07 +0100
commit50850c8d2f94ad346c681b42952f6978a0f6d0ef (patch)
tree1124a89e0bac530f5e5106206006c860a0cbe2e7
parent1daa00990fcb128cd80821f3416a520830bed5de (diff)
Day 7, this time for reals.
-rw-r--r--Day7/app/Main.hs21
1 files changed, 14 insertions, 7 deletions
diff --git a/Day7/app/Main.hs b/Day7/app/Main.hs
index 15bc30a..cb364f4 100644
--- a/Day7/app/Main.hs
+++ b/Day7/app/Main.hs
@@ -99,20 +99,27 @@ convertToAccumulatedSizeTree (Directory name subdirs files) =
DirWithAccumulatedSize name (sum subdirSizes + sum fileSizes) sizedSubdirs
where sizedSubdirs = map convertToAccumulatedSizeTree subdirs
fileSizes = map (\(File _ size) -> size) files
- subdirSizes = map (\(DirWithAccumulatedSize _ size _) -> size) sizedSubdirs
+ subdirSizes = map getSize sizedSubdirs
flattenDirWithAccumulatedSize :: DirWithAccumulatedSize -> [DirWithAcuumulatedSizeFlat]
flattenDirWithAccumulatedSize (DirWithAccumulatedSize name size []) = [DirWithAcuumulatedSizeFlat (name, size)]
flattenDirWithAccumulatedSize (DirWithAccumulatedSize name size (sd:sds)) = flattenDirWithAccumulatedSize sd ++ flattenDirWithAccumulatedSize (DirWithAccumulatedSize name size sds)
solveDay7Part2 :: Directory -> Int
-solveDay7Part2 d = (\(DirWithAcuumulatedSizeFlat (_,s)) -> s) $ smallestDirLargerThan requiredSpace flattenedDirs
+solveDay7Part2 d = getSize $ smallestDirLargerThan requiredSpace flattenedDirs
where flattenedDirs = flattenDirWithAccumulatedSize accumulatedSizeTree
accumulatedSizeTree = convertToAccumulatedSizeTree d
- requiredSpace = rootSize accumulatedSizeTree - (70000000 - 30000000)
- rootSize (DirWithAccumulatedSize _ s _) = s
+ requiredSpace = getSize accumulatedSizeTree - (70000000 - 30000000)
smallestDirLargerThan :: Int -> [DirWithAcuumulatedSizeFlat] -> DirWithAcuumulatedSizeFlat
-smallestDirLargerThan m = foldl1 smallestSize . filter ((m <=) . si) -- too lazy to handle empty list case -> foldl1
- where smallestSize d1 d2 = if si d1 < si d2 then d1 else d2
- si (DirWithAcuumulatedSizeFlat (_,s)) = s \ No newline at end of file
+smallestDirLargerThan m = foldl1 smallestSize . filter ((m <=) . getSize) -- too lazy to handle empty list case -> foldl1
+ where smallestSize d1 d2 = if getSize d1 < getSize d2 then d1 else d2
+
+class DirSize a where
+ getSize :: a -> Int
+
+instance DirSize DirWithAcuumulatedSizeFlat where
+ getSize (DirWithAcuumulatedSizeFlat (_,s)) = s
+
+instance DirSize DirWithAccumulatedSize where
+ getSize (DirWithAccumulatedSize _ s _) = s \ No newline at end of file