import Data.List
main =
[
"1 5 3 6 : 10"
, "1 5 3 6 7 : 10"
, "1 2 3 4 5 : 10"
, "1 2 3 4 5 6 7 8 : 10"
]
find4A s
= filter ((== y
) . eval4A
) exprs
where
exprs
= combo
$ [x1
] :
map addOperators xs
(xs0
, y0
) = break (==':') s
addOperators x
= map (++ x
) [" + ", " - ", " * ", " / "]
combo :: [[[a]]] -> [[a]]
combo (x:xs) = combo' x xs
where
combo' ys [] = ys
combo' ys (h:t) = combo' [y ++ z | y <- ys, z <- h] t
combo _ = []
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
aW1wb3J0IERhdGEuTGlzdAoKbWFpbiA6OiBJTyAoKQptYWluID0KICBtYXBNXyAocHJpbnQgLiBmaW5kNEEpCiAgICAgICAgWwogICAgICAgICAgIjEgNSAzIDYgOiAxMCIKICAgICAgICAsICIxIDUgMyA2IDcgOiAxMCIKICAgICAgICAsICIxIDIgMyA0IDUgOiAxMCIKICAgICAgICAsICIxIDIgMyA0IDUgNiA3IDggOiAxMCIKICAgICAgICBdCgpmaW5kNEEgOjogU3RyaW5nIC0+IFtTdHJpbmddCmZpbmQ0QSBzID0gZmlsdGVyICgoPT0geSkgLiBldmFsNEEpIGV4cHJzCiAgd2hlcmUKICAgIGV4cHJzICAgICA9IGNvbWJvICQgW3gxXSA6IG1hcCBhZGRPcGVyYXRvcnMgeHMKICAgIHgxOnhzICAgICA9IHdvcmRzIHhzMAogICAgeSAgICAgICAgID0gcmVhZCAkIHRhaWwgeTAKICAgICh4czAsIHkwKSA9IGJyZWFrICg9PSc6JykgcwogICAgYWRkT3BlcmF0b3JzIDo6IFN0cmluZyAtPiBbU3RyaW5n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