advent-of-code/2024/day01/day01.hs

19 lines
598 B
Haskell

import Data.List ( sort, group )
import Data.Maybe ( fromMaybe )
import Aoc ( aocMain )
parseFile :: [String] -> ([Int], [Int])
parseFile lines = unzip [(a, b) | line <- lines, let [a, b] = map read $ words line]
part1 :: ([Int], [Int]) -> Int
part1 (list1, list2) = sum $ zipWith (curry (abs . uncurry (-))) (sort list1) (sort list2)
part2 :: ([Int], [Int]) -> Int
part2 (list1, list2) = sum $ map score list1
where
score n = n * fromMaybe 0 (lookup n count)
count = map (\xs -> (head xs, length xs)) $ group $ sort list2
main :: IO ()
main = aocMain parseFile part1 part2