Skip to content

Java8 bot clean #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@
*.exe
*.out
*.app

# JetBrains idea
.idea/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

private static void next_move(int posr, int posc, String[] board){
// if dirty --> clean
if(board[posr].charAt(posc) == 'd'){
System.out.println("CLEAN");
return;
}
// determine the location of the nearest dirty tile
determineClosestDirtyTile(posr, posc, board);
}

private static void determineClosestDirtyTile(int botRow, int botCol, String[] board){
// get the current row
String row = board[botRow];

// check if a dirt spot exists in the row
if(row.contains("d")){
// determine the closest char from left to right
for(int colIdx = 0; colIdx < row.length(); colIdx++){
// get the char in the cell
char cellChar = row.charAt(colIdx);
if(cellChar == 'd'){
if(colIdx < botCol){
System.out.println("LEFT");
return;
}else{
System.out.println("RIGHT");
return;
}
}
}
}
// if no dirt cell found in the row go down
System.out.println("DOWN");
}

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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int gridDimensions;
gridDimensions = in.nextInt();
CellType[][] grid = new CellType[gridDimensions][gridDimensions];
for(int row = 0; row < gridDimensions; row++) {
String rowStr = in.next();
for(int column = 0; column < gridDimensions; column++){
grid[row][column] = CellType.of(rowStr.charAt(column));
}
}

displayPathToPrincess(gridDimensions,grid);
}

private static void displayPathToPrincess(int n, CellType [][] grid){
// Find the location of the bot and princess
int[] princessLocation = getLocationOf(CellType.PRINCESS, grid);
int[] botLocation = getLocationOf(CellType.BOT, grid);

// calculate the row-Diff and column-Diff
int rowDiff = botLocation[0] - princessLocation[0];
int colDiff = princessLocation[1] - botLocation[1];

// print the output
printMovementByDifference(rowDiff, "UP", "DOWN");
printMovementByDifference(colDiff, "RIGHT", "LEFT");
}

private static void printMovementByDifference(int diff, String positiveDirectionName, String negativeDirectionName){
String verticalMovement = diff > 0 ? positiveDirectionName : negativeDirectionName;
diff = diff < 0 ? diff * -1: diff;

for(int i = 0; i < diff; i++) {
System.out.println(verticalMovement);
}
}

private static int[] getLocationOf(CellType cellTypeToSearchFor, CellType [][] grid){
// loop through the rows
for(int rowIdx = 0; rowIdx < grid.length; rowIdx++){
// loop through the columns
for(int columnIdx = 0; columnIdx < grid[rowIdx].length; columnIdx++){
// check the type
if(grid[rowIdx][columnIdx] == cellTypeToSearchFor){
return new int[]{rowIdx, columnIdx};
}
}
}
throw new IllegalArgumentException("No cell of type: " + cellTypeToSearchFor + " in the grid!");
}
}

enum CellType {

EMPTY,
BOT,
PRINCESS;

static CellType of(char cellStrRep){
switch (cellStrRep){
case '-':
return EMPTY;
case 'm':
return BOT;
case 'p':
return PRINCESS;
default:
throw new IllegalArgumentException("No CellType that is identified by " + cellStrRep);
}
}
}