{-# 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]