diff --git a/VERSION b/VERSION index 7b8bb17..9aaad71 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2024.1.1.0 \ No newline at end of file +2024.1.2.0 \ No newline at end of file diff --git a/package.yaml b/package.yaml index 0a80173..1867194 100644 --- a/package.yaml +++ b/package.yaml @@ -502,6 +502,8 @@ executables: aoc-2024-day01: <<: *executable main: AdventOfCode.Year2024.Day01 + dependencies: + - extra _benchmark: &benchmark ghc-options: - -threaded diff --git a/src/AdventOfCode/Year2024/Day01.hs b/src/AdventOfCode/Year2024/Day01.hs index f733bad..37ae9d9 100644 --- a/src/AdventOfCode/Year2024/Day01.hs +++ b/src/AdventOfCode/Year2024/Day01.hs @@ -2,18 +2,28 @@ module AdventOfCode.Year2024.Day01 where import AdventOfCode.Input (parseInput) import AdventOfCode.TH (defaultMain, inputFilePath) +import AdventOfCode.Util (frequencies) import Data.List (sort, transpose) +import Data.List.Extra (sumOn') +import qualified Data.Map as Map import Text.Trifecta (count, natural, some) main :: IO () main = $(defaultMain) -partOne :: (Num a, Ord a) => [[a]] -> a -partOne [xs, ys] = sum $ zipWith ((abs .) . (-)) (sort xs) (sort ys) -partOne _ = error "Ope!" +partOne :: (Num a, Ord a) => ([a], [a]) -> a +partOne (xs, ys) = sum $ zipWith ((abs .) . (-)) (sort xs) (sort ys) -partTwo :: (Num a, Ord a) => [[a]] -> a -partTwo = undefined +partTwo :: (Num a, Ord a) => ([a], [a]) -> a +partTwo (xs, ys) = sumOn' go xs + where + go x = x * fromIntegral (Map.findWithDefault 0 x freqs) + freqs = frequencies ys -getInput :: IO [[Integer]] -getInput = transpose <$> parseInput (some (count 2 natural)) $(inputFilePath) +getInput :: IO ([Integer], [Integer]) +getInput = + do + [xs, ys] <- + transpose + <$> parseInput (some (count 2 natural)) $(inputFilePath) + pure (xs, ys)