From 41e9e71b66949c2694a575c62afd07e1da6ecfa7 Mon Sep 17 00:00:00 2001 From: KoenDR06 Date: Sat, 6 Dec 2025 15:01:18 +0100 Subject: [PATCH] Day 6 --- src/Day2506.hs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/Day2506.hs diff --git a/src/Day2506.hs b/src/Day2506.hs new file mode 100644 index 0000000..6423414 --- /dev/null +++ b/src/Day2506.hs @@ -0,0 +1,54 @@ +module Main where + +import Data.List (find, transpose) +import Data.Maybe (isNothing, isJust) + +splitPred :: (a -> Bool) -> [a] -> [[a]] +splitPred _ [] = [] +splitPred f str | isNothing $ find f str = [str] + | otherwise = takeWhile (not . f) str : splitPred f (tail $ dropWhile (not . f) str) + +part1 :: String -> Int +part1 = sum . map calcOne . transpose . parser + where + parser :: String -> [[String]] + parser = map splitLine . lines + where + splitLine :: String -> [String] + splitLine = filter (not . null) . words + + calcOne :: [String] -> Int + calcOne xs = foldl1 operation operands + where + operation = if last xs == "+" then (+) else (*) + + operands :: [Int] + operands = map read $ drop 1 $ reverse xs + + +part2 :: String -> Int +part2 = sum . map calcOne . map transpose . parse + where + parse = splitPred (all (==' ')) . transpose . lines + + calcOne :: [String] -> Int + calcOne xs = foldl1 operation operands + where + operation = getOperation xs + operands = getOperands xs + + getOperands :: [String] -> [Int] + getOperands xs = map (read) $ transpose $ operands + where + operands = take (length xs - 1) xs + + getOperation :: [String] -> (Int -> Int -> Int) + getOperation xs = if isJust $ find (=='+') $ last xs then (+) else (*) + + +main :: IO () +main = do + str <- getContents + + print $ part1 str + print $ part2 str