Day 3 Part 2

This commit is contained in:
Nathan McCarty 2022-12-03 13:52:10 -05:00
parent 501494ad29
commit aff4844a1e
Signed by: thatonelutenist
GPG Key ID: D70DA3DD4D1E9F96
1 changed files with 20 additions and 1 deletions

View File

@ -44,13 +44,32 @@ parseBackpack input =
duplicatedItems : Backpack -> List Item
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 =
do file <- readFile "input"
case file of
Right 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: "
printLn priorityTotals
putStr "part 2: "
printLn badgeTotal
Left err => printLn err