From a310082d08966cb78bb07f23fbe414614088beb1 Mon Sep 17 00:00:00 2001 From: toastyandwarm Date: Tue, 3 Dec 2024 09:29:48 +0000 Subject: [PATCH] day 3 --- 3/solution.hs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 3/solution.hs diff --git a/3/solution.hs b/3/solution.hs new file mode 100644 index 0000000..81a8b58 --- /dev/null +++ b/3/solution.hs @@ -0,0 +1,24 @@ +import Data.List +import Text.Regex + +solve1 :: String -> Int -> Int +solve1 string curr = + case matchRegexAll (mkRegex "mul\\(([1-9][0-9]?[0-9]?),([1-9][0-9]?[0-9]?)\\)") string of + Nothing -> curr + Just (_, _, rem, matched) -> solve1 (rem) (curr + (foldl (*) 1 (map read matched))) + +solve2 :: String -> Int -> Bool -> Int +solve2 string curr status = + case matchRegexAll (mkRegex "mul\\(([1-9][0-9]?[0-9]?),([1-9][0-9]?[0-9]?)\\)|(do\\(\\))|(don't\\(\\))") string of + Nothing -> curr + Just (_, _, rem, [_, _, "do()", _]) -> solve2 (rem) curr True + Just (_, _, rem, [_, _, _, "don't()"]) -> solve2 (rem) curr False + Just (_, _, rem, matched) -> solve2 (rem) (curr + (if status then (foldl (*) 1 (map read (take 2 matched))) else 0)) status + +main :: IO() +main = do + fileinp <- readFile "input.txt" + let solved1 = solve1 fileinp 0 + let solved2 = solve2 fileinp 0 True + print solved1 + print solved2