Skip to content

Instantly share code, notes, and snippets.

@gubatron
Created August 1, 2013 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gubatron/6132835 to your computer and use it in GitHub Desktop.
Save gubatron/6132835 to your computer and use it in GitHub Desktop.
My HackeRank.com BotClean solution. https://www.hackerrank.com/challenges/botclean
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static List<List<Integer>> getDirtyPositions(String[] board) {
List<List<Integer>> dirtyPositions = new ArrayList<List<Integer>>();
for (int i=0; i < 5; i++) {
String line = board[i];
for (int j=0; j < 5; j++) {
char square = line.charAt(j);
if ((int) square == 100) {
List<Integer> newDirtyPosition = new ArrayList<Integer>();
newDirtyPosition.add(j);
newDirtyPosition.add(i);
dirtyPositions.add(newDirtyPosition);
System.err.println("d @ ("+j+","+i+")");
}
}
}
return dirtyPositions;
}
public static int distanceTo(int fromX, int fromY, List<Integer> target) {
return (int) Math.sqrt((Math.pow(target.get(0) - fromX, 2) + Math.pow(target.get(1) - fromY,2)));
}
public static List<Integer> getClosestDirtyOne(int fromX, int fromY, List<List<Integer>> dirtyOnes) {
List<Integer> result = null;
int shortestDistance = Integer.MAX_VALUE;
for (List<Integer> dirtyOne : dirtyOnes) {
int currentDistance = distanceTo(fromX, fromY, dirtyOne);
if (currentDistance < shortestDistance) {
result = dirtyOne;
shortestDistance = currentDistance;
if (shortestDistance <= 1) {
break;
}
}
}
return result;
}
static void printBoard(String[] board) {
for (int i=0; i < 5; i++) {
System.err.println(board[i]);
}
System.err.println("-----\n");
}
static void printDirtyOnes(List<List<Integer>> dirtyOnes) {
for (List<Integer> xy : dirtyOnes) {
System.err.print("("+xy.get(0)+","+xy.get(1)+"), ");
}
}
static void removeDirtyPosition(List<Integer> toRemove, List<List<Integer>> dirtyPositions) {
Iterator<List<Integer>> iterator = dirtyPositions.iterator();
while (iterator.hasNext()) {
List<Integer> position = iterator.next();
if (position.get(0) == toRemove.get(0) &&
position.get(1) == toRemove.get(1)) {
iterator.remove();
return;
}
}
}
/* Head ends here */
static void next_move(int posx, int posy, String[] board){
//add logic here
List<List<Integer>> dirtyPositions = getDirtyPositions(board);
//while (dirtyPositions.size() > 0) { //leave this if you want to test in one run
if(dirtyPositions.size() > 0) {
List<Integer> closestDirtyOne = getClosestDirtyOne(posx, posy, dirtyPositions);
int dirtX = closestDirtyOne.get(0);
int dirtY = closestDirtyOne.get(1);
int deltaX = dirtX - posx;
int deltaY = dirtY - posy;
int newX = 0;
int newY = 0;
System.err.println("Me ("+posx+","+posy+") - Dirt ("+dirtX+","+dirtY+")");
System.err.println("Cuadratic Distance: " + distanceTo(posx,posy,closestDirtyOne));
String output = null;
if (deltaX == 0 && deltaY == 0) {
output = "CLEAN";
newX = dirtX;
newY =dirtY;
removeDirtyPosition(closestDirtyOne, dirtyPositions);
} else {
if (deltaX > 0) {
output = "RIGHT";
newX = posx + 1;
newY = posy;
} else if (deltaY > 0) {
output = "DOWN";
newY = posy + 1;
newX = posx;
} else if (deltaX < 0) {
output = "LEFT";
newX = posx - 1;
newY = posy;
} else if (deltaY < 0) {
output = "UP";
newY = posy - 1;
newX = posx;
}
}
//update board state in memory if testing with while block.
//System.out.println(output);
//System.err.println("\nBoard before move ["+output+"]");
//printBoard(board);
//char[] line = board[posy].toCharArray();
//line[posx] = '-';
//board[posy] = new String(line);
//line = board[newY].toCharArray();
//line[newX] = 'b';
//board[newY] = new String(line);
System.out.println(output);
//System.err.println("Board after move:");
//printBoard(board);
//printDirtyOnes(dirtyPositions);
//System.err.println("=========================================\n\n");
//posx = newX;
//posy = newY;
}
}
/* Tail starts here */
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment