From 1ad2d43ca1351bf55b7757400da1b3be0b2957f5 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Tue, 3 Dec 2024 10:18:33 +0000 Subject: [PATCH] Done day 3 --- advent03/Main.hs | 69 ++++++++++++++++++++++++++++++++++++++++++++ adventofcode24.cabal | 6 +++- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 advent03/Main.hs diff --git a/advent03/Main.hs b/advent03/Main.hs new file mode 100644 index 0000000..59d64a3 --- /dev/null +++ b/advent03/Main.hs @@ -0,0 +1,69 @@ +-- Writeup at https://work.njae.me.uk/2024/12/03/advent-of-code-2024-day-3/ + +import AoC +import Data.List +import Data.Text (Text) +import qualified Data.Text.IO as TIO +import Data.Attoparsec.Text hiding (take) + + +data Term = Mul Int Int + | Junk Char + | DoTerm + | DontTerm + deriving (Eq, Show) + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- TIO.readFile dataFileName + let program = successfulParse text + -- print program + -- print $ filter notJunk program + -- print $ enabledMuls program + print $ part1 program + print $ part2 program + +part1, part2 :: [Term] -> Int +-- part1 program = sum $ fmap evalTerm program +-- part2 program = sum $ fmap evalTerm $ enabledMuls program +part1 = sum . fmap evalTerm +part2 = sum . fmap evalTerm . enabledMuls + +-- notJunk :: Term -> Bool +-- notJunk (Mul _ _) = True +-- notJunk DoTerm = True +-- notJunk DontTerm = True +-- notJunk _ = False + +evalTerm :: Term -> Int +evalTerm (Mul a b) = a * b +evalTerm _ = 0 + +enabledMuls :: [Term] -> [Term] +enabledMuls terms = snd $ foldl' go (True, []) terms + where go (True, ts) (Mul a b) = (True, Mul a b : ts) + go (_, ts) DontTerm = (False, ts) + go (_, ts) DoTerm = (True, ts) + go (c, ts) _ = (c, ts) + +-- Parse the input file + +junkP, doP, dontP, mulP :: Parser Term +termsP :: Parser [Term] + +junkP = Junk <$> anyChar +-- junksP = Junks <$> many1 anyChar +doP = DoTerm <$ "do()" +dontP = DontTerm <$ "don't()" +mulP = Mul <$> ("mul(" *> decimal <* ",") <*> decimal <* ")" + +-- termsP = (++) <$> (many' junksP) <*> (mulP `sepBy` junksP) +termsP = many' (choice [mulP, doP, dontP, junkP]) + +successfulParse :: Text -> [Term] +successfulParse input = + case parseOnly termsP input of + Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right matches -> matches + diff --git a/adventofcode24.cabal b/adventofcode24.cabal index 7e4d98f..309f2e3 100644 --- a/adventofcode24.cabal +++ b/adventofcode24.cabal @@ -80,4 +80,8 @@ executable advent01 executable advent02 import: warnings, common-extensions, build-directives, common-modules main-is: advent02/Main.hs - \ No newline at end of file + +executable advent03 + import: warnings, common-extensions, build-directives, common-modules + main-is: advent03/Main.hs + build-depends: attoparsec, text -- 2.34.1