From 04a6ec457c92e588ca1b1285f83d7928c85629db Mon Sep 17 00:00:00 2001 From: Gilian Joosen Date: Mon, 14 Oct 2019 11:44:36 +0200 Subject: [PATCH 1/2] added a solution in Java for AI -> Bot Building Challenges -> Bot Saves Princess 1 --- .gitignore | 3 + .../Bot Saves Princess 1-Java8/Solution.java | 76 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 Hackerrank/Artificial Intelligence/Bot Building Challenges/Bot Saves Princess 1-Java8/Solution.java diff --git a/.gitignore b/.gitignore index 4581ef2..31ba49a 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,6 @@ *.exe *.out *.app + +# JetBrains idea +.idea/* \ No newline at end of file diff --git a/Hackerrank/Artificial Intelligence/Bot Building Challenges/Bot Saves Princess 1-Java8/Solution.java b/Hackerrank/Artificial Intelligence/Bot Building Challenges/Bot Saves Princess 1-Java8/Solution.java new file mode 100644 index 0000000..88ebe4a --- /dev/null +++ b/Hackerrank/Artificial Intelligence/Bot Building Challenges/Bot Saves Princess 1-Java8/Solution.java @@ -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); + } + } +} \ No newline at end of file From 439a56a4283ab41cab9c8cf532299a29256cc366 Mon Sep 17 00:00:00 2001 From: Gilian Joosen Date: Mon, 14 Oct 2019 13:04:21 +0200 Subject: [PATCH 2/2] added a Java8 solution for Artificial Intelligence -> Bot Building -> Bot clean --- .../Bot Clean-Java8/Solution.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Hackerrank/Artificial Intelligence/Bot Building Challenges/Bot Clean-Java8/Solution.java diff --git a/Hackerrank/Artificial Intelligence/Bot Building Challenges/Bot Clean-Java8/Solution.java b/Hackerrank/Artificial Intelligence/Bot Building Challenges/Bot Clean-Java8/Solution.java new file mode 100644 index 0000000..0eaf063 --- /dev/null +++ b/Hackerrank/Artificial Intelligence/Bot Building Challenges/Bot Clean-Java8/Solution.java @@ -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); + } +} \ No newline at end of file