Day 3 Part 2
This commit is contained in:
parent
501494ad29
commit
aff4844a1e
21
03/Main.idr
21
03/Main.idr
|
@ -44,13 +44,32 @@ parseBackpack input =
|
||||||
duplicatedItems : Backpack -> List Item
|
duplicatedItems : Backpack -> List Item
|
||||||
duplicatedItems (B left right) = nub $ intersect left right
|
duplicatedItems (B left right) = nub $ intersect left right
|
||||||
|
|
||||||
|
-- Shared items across a collection of backpack
|
||||||
|
shared : List Backpack -> List Item
|
||||||
|
shared xs =
|
||||||
|
let combined = map (\(B left right) => left ++ right ) xs
|
||||||
|
in nub . intersectAll $ combined
|
||||||
|
|
||||||
|
-- Chunk a list
|
||||||
|
chunk : Nat -> List a -> List (List a)
|
||||||
|
chunk x [] = []
|
||||||
|
chunk x xs =
|
||||||
|
let head = take x xs
|
||||||
|
tail = chunk x . drop x $ xs
|
||||||
|
in head :: tail
|
||||||
|
|
||||||
main : IO ()
|
main : IO ()
|
||||||
main =
|
main =
|
||||||
do file <- readFile "input"
|
do file <- readFile "input"
|
||||||
case file of
|
case file of
|
||||||
Right content =>
|
Right content =>
|
||||||
let backpacks = catMaybes . map parseBackpack . lines $ content
|
let backpacks = catMaybes . map parseBackpack . lines $ content
|
||||||
priorityTotals = foldl (+) 0 . map (foldl (+) 0 . map priority . duplicatedItems) $ backpacks
|
priorityTotals = sum . map (sum . map priority . duplicatedItems) $ backpacks
|
||||||
|
elves = chunk 3 backpacks
|
||||||
|
badges = map shared elves
|
||||||
|
badgeTotal = sum . map (sum . map priority) $ badges
|
||||||
in do putStr "part 1: "
|
in do putStr "part 1: "
|
||||||
printLn priorityTotals
|
printLn priorityTotals
|
||||||
|
putStr "part 2: "
|
||||||
|
printLn badgeTotal
|
||||||
Left err => printLn err
|
Left err => printLn err
|
||||||
|
|
Loading…
Reference in New Issue