From: Neil Smith Date: Sun, 1 Dec 2024 10:18:39 +0000 (+0000) Subject: Done day 1 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=e4ca14b9e497e4db9e5ef502ba06ab1882a12b6c;p=advent-of-code-24.git Done day 1 --- diff --git a/advent01/Main.hs b/advent01/Main.hs new file mode 100644 index 0000000..b304759 --- /dev/null +++ b/advent01/Main.hs @@ -0,0 +1,30 @@ +-- Writeup at https://work.njae.me.uk/2023/12/01/advent-of-code-2023-day-01/ + +import AoC +-- import Data.Char +import Data.List +import qualified Data.MultiSet as MS + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- readFile dataFileName + let pairs = fmap readPair $ lines text + print pairs + print $ part1 pairs + print $ part2 pairs + +part1 :: [(Int, Int)] -> Int +part1 pairs = sum $ zipWith absDiff (sort lefts) (sort rights) + where (lefts, rights) = unzip pairs + absDiff a b = abs (a - b) + +part2 pairs = sum $ fmap similarity lefts + where (lefts, rights) = unzip pairs + counts = MS.fromList rights + similarity l = l * (MS.occur l counts) + +readPair :: String -> (Int, Int) +readPair s = (read a, read b) + where (a : b : _ ) = words s + diff --git a/adventofcode24.cabal b/adventofcode24.cabal index 6f98911..8126db1 100644 --- a/adventofcode24.cabal +++ b/adventofcode24.cabal @@ -70,6 +70,6 @@ executable adventofcode24 default-language: GHC2021 executable advent01 - import: common-extensions, build-directives + import: warnings, common-extensions, build-directives main-is: advent01/Main.hs --- build-depends: split + build-depends: multiset diff --git a/app/Main.hs b/app/Main.hs index 60d904e..0600f81 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,8 +1,8 @@ module Main where -import qualified MyLib (someFunc) +-- import qualified MyLib (someFunc) main :: IO () main = do putStrLn "Hello, Haskell!" - MyLib.someFunc + -- MyLib.someFunc diff --git a/src/AoC.hs b/src/AoC.hs index 449c739..90b5c62 100644 --- a/src/AoC.hs +++ b/src/AoC.hs @@ -1,4 +1,4 @@ -Module AoC ( getDataFileName ) where +module AoC ( getDataFileName ) where import System.Environment @@ -9,8 +9,8 @@ getDataFileName = let baseDataName = if null args then progName else head args - let baseDataName' = if length baseDataName < 5 - then progName ++ baseDataName - else baseDataName + let baseDataName' = if length baseDataName < 5 + then progName ++ baseDataName + else baseDataName let dataFileName = "../data/" ++ baseDataName' ++ ".txt" return dataFileName