Hackerrank in R: Bot Saves Princess – 2

Bot Saves Princess-2 problem in Hackerrank is more or less similar to the first  Bot Saves Princess problem. But it took me some time to understand what the problem is and what is the expected solution. Well, the solution to the 1st problem is here.

 
 

Problem:

In this version of “Bot saves princess”, Princess Peach and bot’s position are randomly set.  Our task is to output one step at a time to reach the Princess and save her. Now, let’s take a look at the input format and understand what we need to do.

Input format:

5
2 3 
—– 
—– 
—m- 
p—- 
—–

The first line contains an integer “n” denoting the size of the grid. The second line of the input contains two space-separated integers, which is the position of the bot.  The bot position is denoted by ‘m’ and the princess position is denoted by ‘p’. The board follows the matrix convention of Mathematics. A board of size n x n has top left indexed (0,0) and bottom right indexed (n-1,n-1). The row index increases from top to bottom(i.e. 0 to n-1), and the column index increases from left to right( i.e. 0 to n-1).

For this example,  the bot is at (2,3) and the princess is at (3,0). So the output is expected to be either “DOWN” or “Left” i.e. one step closer to p. 

Code1:

Lets read the input first.

# Enter your code here. Read input from STDIN. Print output to STDOUT
stdin <- file('input')
open(stdin)
input <- readLines(stdin, warn = FALSE)
input <- unlist(strsplit(input, split = " "))
n <- as.integer(input[1])
r <- as.integer(input[2])
c <- as.integer(input[3])
grid <- input

At this point, the input looks like below: 
5 2 3 —– —– —m- p—- —–
The class of the input string is “character” :
n  = 5
r   = 2
c   = 3
grid =  “5 2 3 —– —– —m- p—- —–“

Now, let’s define the function “nextMove”. The input parameters o the function are “n” – the size of the matrix, “r” the row where the Bot is and “c” the column position of the bot and “grid” to read the princess’s position.

nextMove <- function(n, r , c, grid){
 t <- n * n
 for( i in 1:t){
 inputline <- unlist(strsplit(grid,""))[i + 3]
 if (inputline == "p"){
 a = as.integer(i)
 if (a <= n){
 rp = 0
 cp = a-1
 }
 else if (a%%n == 0){
 rp = (a/n) - 1
 cp = 4
 }
 else{
 rp = floor(a/n)
 cp = (a%%n) -1
 }
 }
 }
 dir <- ' '
 if(r != rp){
 if(r-rp>0){
 dir = "UP"
 }
 else{
 dir = "DOWN"
 }
 }
 else{
 if(c - cp >0){
 dir = "LEFT"
 }
 else{
 dir = "RIGHT"
 } 
 }
 
 n <- 0
 r <- 0
 c <- 0
 cat(dir)
}

Well, even I didn’t like my logic in the above function and I am sure there must be some smart way of doing it. But I can assure the logic is correct and working.

Let me clarify my reasoning:

Logical Steps:

  1. “Inputline” is reading all the characters individually. i.e. if we have a 5 * 5 matrix, we have got “t” = 5 * 5 = 25 characters we need to loop through and check if we got a “p”. 
  2. Suppose we got a princess “p” at position “i”.  Now, we need to find the row and column position of princess “p”. 

  • if (a <= n) => If “p” is at some position number less than “n” (for example a = 4) , that means “p” is at the 1st row i.e. rp = 0 and column cp = 4-1 = 3 (since the matrix is indexed at (0,0).
  • else if (a%%n == 0) => If “p” is at some position number which is exactly divisable by n ( for example a = 20), that means “p” is at the last column i.e. cp = 4 and row rp = (20/5) -1 = 3
  • else condition: If “p” is at some position number which is not satisfied by the above two conditions(for this example a= 16) then the row position rp = floor(a/n) = floor(16/5) = 3 and the column position cp = (a%%n) -1 = (16 %% 5) – 1 = 1-1 =0.

 3. Once we get the row position “rp” and column position “cp” of the princess, we can decide the direction of the Bot movement.
 if(r != rp) i.e. bot and princess are not in the same row, 

  • if(r-rp>0) => move “UP”
  • else => move “DOWN”

 if bot and princess are in the same row, we will check the column position:

  •  if(c – cp >0) => move “LEFT”
  • else => move “RIGHT”

Now, let’s call the function for each input and get an output:

nextMove(n, r , c, grid)
close(stdin)

Thus, for each run, we will check the input and will return only one step based on the positions of the bot and the princess. We will play the game until the Bot reaches the princess.

If you have a better solution in R, please do share with me. Until then, happy coding and “Thank You” for reading!

0

Leave a Reply

Your email address will not be published. Required fields are marked *