Advent of Code 2024 - Day 1
parent
51ae128a14
commit
4906e74cf3
|
@ -0,0 +1,38 @@
|
||||||
|
-- https://adventofcode.com/2024/day/1
|
||||||
|
|
||||||
|
import Data.List
|
||||||
|
import GHC.IO
|
||||||
|
|
||||||
|
input :: FilePath
|
||||||
|
input = "src/advent_of_code/2024/1.input"
|
||||||
|
|
||||||
|
entries :: ([Int], [Int])
|
||||||
|
entries = unzip $ map parse' $ unsafePerformIO $ lines <$> readFile input
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
entries <- lines <$> readFile input
|
||||||
|
print "Advent of Code 2024 - Day 1"
|
||||||
|
|
||||||
|
print $ "Part 1: " <> show (solveP1 $ unzip $ map parse' entries)
|
||||||
|
print $ "Part 2: " <> show (solveP2 $ unzip $ map parse' entries)
|
||||||
|
|
||||||
|
parse' :: String -> (Int, Int)
|
||||||
|
parse' str = (read l, read r)
|
||||||
|
where
|
||||||
|
l = take' str
|
||||||
|
r = reverse $ take' $ reverse str
|
||||||
|
take' = takeWhile (/= ' ')
|
||||||
|
|
||||||
|
solveP1 :: ([Int], [Int]) -> Int
|
||||||
|
solveP1 (as, bs) = sum $ zipWith (\i j -> abs (i - j)) as' bs'
|
||||||
|
where
|
||||||
|
as' = sort as
|
||||||
|
bs' = sort bs
|
||||||
|
|
||||||
|
solveP2 :: ([Int], [Int]) -> Int
|
||||||
|
solveP2 (as, bs) = sum $ zipWith (*) as (map (f bs) as)
|
||||||
|
where
|
||||||
|
f :: [Int] -> Int -> Int
|
||||||
|
f bs a = length $ elemIndices a bs
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,6 @@
|
||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
Loading…
Reference in New Issue