Created
December 2, 2024 02:15
-
-
Save skatenerd/5370f3468da46fd075153dcbe0ae157f to your computer and use it in GitHub Desktop.
2024 Day One
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module DayOne (partOne, partTwo, testLines) where | |
import qualified Text.Read as TR | |
import qualified Data.Text as T | |
import qualified Data.List as L | |
import qualified Test.LeanCheck.Stats as TS | |
import qualified Data.Map as DM | |
testLines :: [T.Text] | |
testLines = [ "3 4", "4 3", "2 5", "1 3", "3 9", "3 3" ] | |
parseLine :: T.Text -> (Int, Int) | |
parseLine line = (TR.read (T.unpack a), TR.read (T.unpack b)) | |
where (a:(b:_)) = T.words line | |
partOne :: [T.Text] -> Int | |
partOne inputLines = sum scores | |
where tuples = fmap parseLine inputLines | |
firsts = L.sort $ fmap fst tuples | |
seconds = L.sort $ fmap snd tuples | |
pairs = zip firsts seconds | |
scores = fmap scorePair pairs | |
scorePair (a,b) = abs $ a - b | |
partTwo :: [T.Text] -> Int | |
partTwo inputLines = sum scores | |
where tuples = fmap parseLine inputLines | |
firsts = fmap fst tuples | |
seconds = fmap snd tuples | |
countLookup = DM.fromList $ TS.counts seconds | |
score n = n * DM.findWithDefault 0 n countLookup | |
scores = fmap score firsts | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment