From a74271d093a3bcb05c75fa4a5e40ff60665ecdb6 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 9 Dec 2024 20:09:29 +0100 Subject: 7 --- 2024/7.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2024/7.hs (limited to '2024') diff --git a/2024/7.hs b/2024/7.hs new file mode 100644 index 0000000..adcec6b --- /dev/null +++ b/2024/7.hs @@ -0,0 +1,22 @@ +{-# OPTIONS -Wno-incomplete-uni-patterns #-} +import Control.Monad (guard) + +main :: IO () +main = do + let parse line = let (lhs, _ : rhs) = break (== ':') line + in (read @Int lhs, map (read @Int) (words rhs)) + input <- map parse . lines <$> getContents + + let numdigs = go 1 + where go !d n | n < 10 = d :: Int + | otherwise = go (d+1) (n `quot` 10) + let a .|| b = a * 10 ^ numdigs b + b + + let evals _ _ acc [] = [acc] + evals p2 lhs acc (n:nums) = do + acc' <- [acc + n, acc * n] ++ (if p2 then [acc .|| n] else []) + guard (acc' <= lhs) + evals p2 lhs acc' nums + + print $ sum [lhs | (lhs, nums) <- input, lhs `elem` evals False lhs 0 nums] + print $ sum [lhs | (lhs, nums) <- input, lhs `elem` evals True lhs 0 nums] -- cgit v1.2.3-70-g09d2