-
-
Save charypar/b3b4ed981e9f3075e80e to your computer and use it in GitHub Desktop.
Put the input in a text file. Then in the variable path (in main method), add the path to the text file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.stream.Stream; | |
public class Robot { | |
private static int gridX; | |
private static int gridY; | |
private static HashMap<String, Integer> scents = new HashMap<>(); | |
/* | |
the directions currently correspond to [N, E, S, W] | |
they are in clockwise direction | |
if NE has to be added then {1,1} can be added as second element | |
to give corresponding directions as [N, NE, E, S, W] | |
{1, 1} because if the robot moves NE then it will increase both its x and y coordinate | |
*/ | |
private static int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; | |
private int x; | |
private int y; | |
private boolean isFallen = false; | |
private int directionIndex = 0; | |
public Robot(int x, int y, char orient){ | |
this.x = x; | |
this.y = y; | |
switch(orient){ | |
case 'N': this.directionIndex = 0; | |
break; | |
case 'E': this.directionIndex = 1; | |
break; | |
case 'S': this.directionIndex = 2; | |
break; | |
case 'W': this.directionIndex = 3; | |
break; | |
} | |
} | |
public static void setGridSize(int gx, int gy){ | |
gridX = gx; | |
gridY = gy; | |
} | |
private char currentOrientation(){ | |
switch (this.directionIndex){ | |
case 0: return 'N'; | |
case 1: return 'E'; | |
case 2: return 'S'; | |
default: return 'W'; | |
} | |
} | |
public void changeOrientation(char a) { | |
switch (a) { | |
case 'R': | |
directionIndex++; | |
directionIndex %= directions.length; | |
break; | |
case 'L': | |
if (directionIndex == 0) | |
directionIndex = directions.length - 1; | |
else | |
directionIndex--; | |
break; | |
} | |
} | |
public boolean forward() { | |
String add = "" + this.x + this.y + directions[directionIndex][0] + directions[directionIndex][1]; | |
if (scents.containsKey(add)) { | |
return true; | |
} | |
int newX = this.x + directions[directionIndex][0]; | |
int newY = this.y + directions[directionIndex][1]; | |
if (newX > gridX || newY > gridY) { | |
scents.put(add, 1); | |
this.isFallen = true; | |
return false; | |
} | |
this.x = newX; | |
this.y = newY; | |
return true; | |
} | |
public boolean move(char a) { | |
switch (a) { | |
case 'F': | |
return this.forward(); | |
case 'R': | |
this.changeOrientation(a); | |
return true; | |
case 'L': | |
this.changeOrientation(a); | |
return true; | |
} | |
return false; | |
} | |
public String toString(){ | |
return this.x + " " + this.y + " " + this.currentOrientation() + " " + (this.isFallen ? "LOST" : ""); | |
} | |
public static String launch(Robot rob, String input){ | |
for(int i = 0; i<input.length(); i++){ | |
if(!rob.move(input.charAt(i))){ | |
return rob.toString(); | |
} | |
} | |
return rob.toString(); | |
} | |
public static void main (String args[]){ | |
String path = ""; | |
ArrayList<String> input = new ArrayList<>(); | |
try (Stream<String> stream = Files.lines(Paths.get(path))){ | |
stream.filter(p -> !p.equals("")).forEach(input::add); | |
} catch (IOException e){ | |
e.printStackTrace(); | |
} | |
ArrayList<String> temp = answer(input); | |
for(String i: temp){ | |
System.out.println(i); | |
} | |
} | |
public static ArrayList<String> answer(ArrayList<String> input){ | |
String[] one = input.get(0).split(" "); | |
Robot.setGridSize(Integer.parseInt(one[0]), Integer.parseInt(one[1])); | |
ArrayList<String> ans = new ArrayList<>(); | |
for(int i = 1; i<input.size()-1; i+=2){ | |
String[] init = input.get(i).split(" "); | |
Robot rob = new Robot(Integer.parseInt(init[0]), Integer.parseInt(init[1]), init[2].charAt(0)); | |
String a = Robot.launch(rob, input.get(i+1)); | |
ans.add(a); | |
} | |
return ans; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment