NicolaBernini / CodingChallengesSolutions

CPP Code Snippets with comments
4 stars 4 forks source link

Hackerrank - Cats and a Mouse #31

Open NicolaBernini opened 4 years ago

NicolaBernini commented 4 years ago

Overview

Solution to the Cats and a Mouse in different languages

NicolaBernini commented 4 years ago

Scala

import java.io._
import java.math._
import java.security._
import java.text._
import java.util._
import java.util.concurrent._
import java.util.function._
import java.util.regex._
import java.util.stream._

object Solution {

    // Complete the catAndMouse function below.
    def catAndMouse(x: Int, y: Int, z: Int): String = {
    ( (x-z).abs, (y-z).abs ) match {
        case (d1, d2) if d1 < d2 => "Cat A"
        case (d1, d2) if d1 > d2 => "Cat B"
        case (d1, d2) if d1==d2 => "Mouse C"
    }

    }

    def main(args: Array[String]) {
        val stdin = scala.io.StdIn

        val printWriter = new PrintWriter(sys.env("OUTPUT_PATH"))

        val q = stdin.readLine.trim.toInt

        for (qItr <- 1 to q) {
            val xyz = stdin.readLine.split(" ")

            val x = xyz(0).trim.toInt

            val y = xyz(1).trim.toInt

            val z = xyz(2).trim.toInt

            val result = catAndMouse(x, y, z)

            printWriter.println(result)
        }

        printWriter.close()
    }
}