• + 0 comments

    scala:

    import scala.io.StdIn
    
    object Solution {
      private def checkLeft(id: BigInt): Boolean = {
        if (id < 10)
          id.isProbablePrime(100)
        else if (!id.isProbablePrime(100))
          false
        else
          checkLeft(BigInt(id.toString.tail))
      }
    
      private def checkRight(id: BigInt): Boolean = {
        if (id < 1)
          true
        else if (!id.isProbablePrime(100))
          false
        else
          checkRight(id / 10)
      }
    
      private def throwToSea(troops: Seq[BigInt]): Seq[String] = {
        troops.map(id =>
          if (id.toString.contains('0'))
            "DEAD"
          else {
            val left = checkLeft(id)
            val right = checkRight(id)
            if (left && right)
              "CENTRAL"
            else if (left)
              "LEFT"
            else if (right)
              "RIGHT"
            else
              "DEAD"
          }
        )
      }
    
      def main(args: Array[String]): Unit = {
        val in = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        val t = in.next().toInt
        val troops = (1 to t)
          .map(_ => BigInt(in.next()))
        println(throwToSea(troops).mkString("\n"))
      }
    }