2024-12-04 17:15:59 +11:00
|
|
|
{-# OPTIONS_GHC -Wno-x-partial #-}
|
|
|
|
|
|
|
|
import Aoc
|
2024-12-02 18:55:47 +11:00
|
|
|
import Data.List ( sort, group )
|
|
|
|
import Data.Maybe ( fromMaybe )
|
|
|
|
|
|
|
|
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
|