import Data.List main :: IO () main = mapM_ (print . find4A) ["1 5 3 6 : 10", "1 5 3 6 7 : 10"] find4A :: String -> [String] find4A s = (flip (map . (!!)) =<< elemIndices y . map expr) $ combo [x1] opPlusDigitList where combo as [] = as combo as (h:t) = combo [a ++ b | a <- as, b <- h] t opPlusDigitList = map (\x -> [op ++ x | op <- ops]) $ xs (xs0, y0) = break (==':') s x1:xs = words xs0 y = read $ tail y0 :: Double ops = [" + ", " - ", " * ", " / "] expr :: String -> Double expr s = go (map read xxs) yys where (xxs, yys) = partition (`notElem` ["+", "-", "*", "/"]) $ words s go [x1] _ = x1 go (x1:x2:xs) ("*":ys) = go ((x1 * x2) : xs) ys go (x1:x2:xs) ("/":ys) = go ((x1 / x2) : xs) ys go (x1:xs) ("+":ys) = x1 + go xs ys go (x1:x2:xs) ("-":ys) = x1 + go ((-x2) : xs) ys go _ _ = -1