-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2157.hs
More file actions
28 lines (22 loc) · 780 Bytes
/
2157.hs
File metadata and controls
28 lines (22 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Control.Monad
import Control.Applicative
import qualified Control.Exception as E
import Data.List
safeRead :: String -> Maybe Int
safeRead s =
case reads s of
[(x, "")] -> Just x
_ -> Nothing
getABC = (\ [a,_,b,_,c] -> (a,b,c)) . words
monadPowers (a,b,c) =
let [ma',mb',mc'] = map safeRead [a,b,c]
ma'' = (-) <$> mc' <*> mb'
mb'' = (-) <$> mc' <*> ma'
mc'' = (+) <$> ma' <*> mb'
in maybe [] id $ zipWithM mplus [ma',mb',mc'] [ma'',mb'',mc'']
pp [a,b,c] = concat [show a, " + ", show b, " = ", show c]
getOddLines = map snd . filter fst . zip (cycle [True,False])
main = do
l:ls <- getOddLines . lines <$> getContents
mapM_ (putStrLn . pp . monadPowers . getABC) $ take (read l) ls
return ()