BotClean

I received a message from HackerRank suggesting the BotClean challenge. If interested take a look at the requirements at the following URL:  https://www.hackerrank.com/challenges/botclean

I had an issue with the Output Format description. Some time ago I looked at a different challenge and my solution generated a set of steps in order to complete each task. In this case only one step may be displayed at a time. Seems that the code that invokes the method next_move() updates the coordinates and the board each time after executing a move.

Following is a screen capture of the console of the Eclipse IDE using the first sample data:

0 0

b—d

-d–d

–dd-

–d–

—-d

DOWN

The bot is located at the top left corner of the board. The suggested output is RIGHT but as one can see from the board, DOWN should also be considered a valid answer.

The following screen capture illustrates the second sample data:

0 1

-b–d

-d–d

–dd-

–d–

—-d

DOWN

In this case the only possible move from (0,1) is DOWN to (1,1).

Following is my solution which was accepted:

import java.util.Scanner;

public class Solution {

/*

* Determine location of closest dirty point.

* Generate a single move per invocation.

*/

static void next_move(int row, int col, String[] board) {

int minRow    = -1;

int minCol    = -1;

int minDist   = Integer.MAX_VALUE;

int delta     = 0;

// **** determine if current cell is dirty ****

if (board[row].substring(col, col + 1).equals(“d”)) {

System.out.println(“CLEAN”);

                     // **** only one move per invocation ****                    

                     return;

}

// **** determine closest dirty point ****

for (int r = 0; r < board.length; r++) {

for (int c = 0; c < board.length; c++) {

// **** determine if this is a dirty cell ****

if (board[r].substring(c, c + 1).equals(“d”)) {

// **** update min distance, row and column values ****

int dist = (row – r) * (row – r) + (col – c) * (col – c);

if (dist < minDist) {

minDist = dist;

minRow        = r;

minCol        = c;

}

}

}

}

// **** move closer to dirty point (y-axis) ****

delta = minRow – row;

delta = (delta <= 0) ? -1 : 1;

for (int r = row; r != minRow; r += delta) {

if (row < minRow) {

System.out.println(“DOWN”);

} else {

System.out.println(“UP”);

}

                     // **** only one move per invocation ****

                     return;

}

// ***** move closer to dirty point (x-axis) ****

delta = minCol – col;

delta = (delta <= 0) ? -1 : 1;

for (int c = col; c != minCol; c += delta) {

if (col < minCol) {

System.out.println(“RIGHT”);

} else {

System.out.println(“LEFT”);

}

                     // **** only one move per invocation ****

                     return;

}

}

/*

* Test code.

*/

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int [] pos = new int[2];

String board[] = new String[5];

for(int i=0;i<2;i++) pos[i] = in.nextInt();

for(int i=0;i<5;i++) board[i] = in.next();

next_move(pos[0], pos[1], board);

// **** close scanner ****

in.close();

}

}

As you can see in the code I inserted the following line in the code at three points in order to perform a single operation per function invocation:

// **** only one move per invocation ****

return;

If you have comments or questions regarding this or any other entry in this blog please do not hesitate and send me a message. I will not use your name unless you explicitly allow me to do so.

John

john.canessa@gmail.com

Follow me on Twitter:  john_canessa

Leave a Reply

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