Done some puzzles
[cses-programming-tasks.git] / app / cses1071.hs
diff --git a/app/cses1071.hs b/app/cses1071.hs
new file mode 100644 (file)
index 0000000..22f610a
--- /dev/null
@@ -0,0 +1,38 @@
+main :: IO ()
+main = do
+  line1 <- getLine
+  let numQuestions = read line1 :: Int
+  answers <- forM [1..numQuestions] $ \_ -> do
+    q <- getQuestion
+    return $ solve q
+  putStrLn $ unlines $ map show answers
+
+getQuestion :: IO (Int, Int)
+getQuestion = do
+  line <- getLine
+  let question = map read $ words line
+  return (question !! 0, question !! 1)
+
+
+solve :: (Int, Int) -> Int
+solve (row, col) = go oddTarget
+  where maxRC = max row col
+        oddTarget = (maxRC `mod` 2) == 1
+        target = maxRC * maxRC
+        go True
+          | col >= maxRC = target - (row - 1)
+          | otherwise = (target - (row - 1)) - (row - col)
+        go False
+          | row >= maxRC = target - (col - 1)
+          | otherwise = (target - (col - 1)) - (col - row)
+
+-- if target is odd and col >= maxRC:
+--   target - (row - 1)
+-- if target is odd and row < maxRC:
+--   (target - (row - 1)) - (row - col)
+
+-- if target is even and row >= maxRC
+--   target - (col - 1)
+-- if target is even and col < maxRC
+--   (target - (col - 1)) - (col - row)
+