Skip to content

Commit

Permalink
feat(2024.01-haskell): solve Part Two naively
Browse files Browse the repository at this point in the history
  • Loading branch information
yurrriq committed Dec 3, 2024
1 parent 519cbfc commit d6981e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.1.1.0
2024.1.2.0
2 changes: 2 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ executables:
aoc-2024-day01:
<<: *executable
main: AdventOfCode.Year2024.Day01
dependencies:
- extra
_benchmark: &benchmark
ghc-options:
- -threaded
Expand Down
24 changes: 17 additions & 7 deletions src/AdventOfCode/Year2024/Day01.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d6981e8

Please sign in to comment.