--- /dev/null
+-- 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
+
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
module Main where
-import qualified MyLib (someFunc)
+-- import qualified MyLib (someFunc)
main :: IO ()
main = do
putStrLn "Hello, Haskell!"
- MyLib.someFunc
+ -- MyLib.someFunc
-Module AoC ( getDataFileName ) where
+module AoC ( getDataFileName ) where
import System.Environment
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