Tackled problem 1625
[cses-programming-tasks.git] / app / cses1071.hs
1 main :: IO ()
2 main = do
3 line1 <- getLine
4 let numQuestions = read line1 :: Int
5 answers <- forM [1..numQuestions] $ \_ -> do
6 q <- getQuestion
7 return $ solve q
8 putStrLn $ unlines $ map show answers
9
10 getQuestion :: IO (Int, Int)
11 getQuestion = do
12 line <- getLine
13 let question = map read $ words line
14 return (question !! 0, question !! 1)
15
16
17 solve :: (Int, Int) -> Int
18 solve (row, col) = go oddTarget
19 where maxRC = max row col
20 oddTarget = (maxRC `mod` 2) == 1
21 target = maxRC * maxRC
22 go True
23 | col >= maxRC = target - (row - 1)
24 | otherwise = (target - (row - 1)) - (row - col)
25 go False
26 | row >= maxRC = target - (col - 1)
27 | otherwise = (target - (col - 1)) - (col - row)
28
29 -- if target is odd and col >= maxRC:
30 -- target - (row - 1)
31 -- if target is odd and row < maxRC:
32 -- (target - (row - 1)) - (row - col)
33
34 -- if target is even and row >= maxRC
35 -- target - (col - 1)
36 -- if target is even and col < maxRC
37 -- (target - (col - 1)) - (col - row)
38