Skip to content

Commit d6981e8

Browse files
committed
feat(2024.01-haskell): solve Part Two naively
1 parent 519cbfc commit d6981e8

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2024.1.1.0
1+
2024.1.2.0

package.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@ executables:
502502
aoc-2024-day01:
503503
<<: *executable
504504
main: AdventOfCode.Year2024.Day01
505+
dependencies:
506+
- extra
505507
_benchmark: &benchmark
506508
ghc-options:
507509
- -threaded

src/AdventOfCode/Year2024/Day01.hs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@ module AdventOfCode.Year2024.Day01 where
22

33
import AdventOfCode.Input (parseInput)
44
import AdventOfCode.TH (defaultMain, inputFilePath)
5+
import AdventOfCode.Util (frequencies)
56
import Data.List (sort, transpose)
7+
import Data.List.Extra (sumOn')
8+
import qualified Data.Map as Map
69
import Text.Trifecta (count, natural, some)
710

811
main :: IO ()
912
main = $(defaultMain)
1013

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)
1416

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
1722

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

Comments
 (0)