@@ -2,18 +2,28 @@ module AdventOfCode.Year2024.Day01 where
2
2
3
3
import AdventOfCode.Input (parseInput )
4
4
import AdventOfCode.TH (defaultMain , inputFilePath )
5
+ import AdventOfCode.Util (frequencies )
5
6
import Data.List (sort , transpose )
7
+ import Data.List.Extra (sumOn' )
8
+ import qualified Data.Map as Map
6
9
import Text.Trifecta (count , natural , some )
7
10
8
11
main :: IO ()
9
12
main = $ (defaultMain)
10
13
11
- partOne :: (Num a , Ord a ) => [[a ]] -> a
12
- partOne [xs, ys] = sum $ zipWith ((abs . ) . (-) ) (sort xs) (sort ys)
13
- partOne _ = error " Ope!"
14
+ partOne :: (Num a , Ord a ) => ([a ], [a ]) -> a
15
+ partOne (xs, ys) = sum $ zipWith ((abs . ) . (-) ) (sort xs) (sort ys)
14
16
15
- partTwo :: (Num a , Ord a ) => [[a ]] -> a
16
- partTwo = undefined
17
+ partTwo :: (Num a , Ord a ) => ([a ], [a ]) -> a
18
+ partTwo (xs, ys) = sumOn' go xs
19
+ where
20
+ go x = x * fromIntegral (Map. findWithDefault 0 x freqs)
21
+ freqs = frequencies ys
17
22
18
- getInput :: IO [[Integer ]]
19
- getInput = transpose <$> parseInput (some (count 2 natural)) $ (inputFilePath)
23
+ getInput :: IO ([Integer ], [Integer ])
24
+ getInput =
25
+ do
26
+ [xs, ys] <-
27
+ transpose
28
+ <$> parseInput (some (count 2 natural)) $ (inputFilePath)
29
+ pure (xs, ys)
0 commit comments