Created
December 9, 2016 12:02
-
-
Save ankitagrawal2411/5887f214ade17ba718b8fac7499d0946 to your computer and use it in GitHub Desktop.
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.util.ArrayList; | |
import java.util.HashMap; | |
public class Main { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
String input ="R2, L5, L4, L5, R4, R1, L4, R5, R3, R1, L1, L1, R4," | |
+ " L4, L1, R4, L4, R4, L3, R5, R4, R1, R3, L1, L1, R1, L2," | |
+ " R5, L4, L3, R1, L2, L2, R192, L3, R5, R48, R5, L2, R76, " | |
+ "R4, R2, R1, L1, L5, L1, R185, L5, L1, R5, L4, R1, R3, L4," | |
+ " L3, R1, L5, R4, L4, R4, R5, L3, L1, L2, L4, L3, L4, R2, " | |
+ "R2, L3, L5, R2, R5, L1, R1, L3, L5, L3, R4, L4, R3, L1, R5," | |
+ " L3, R2, R4, R2, L1, R3, L1, L3, L5, R4, R5, R2, R2, L5, " | |
+ "L3, L1, L1, L5, L2, L3, R3, R3, L3, L4, L5, R2, L1, R1, R3," | |
+ " R4, L2, R1, L1, R3, R3, L4, L2, R5, R5, L1, R4, L5, L5, R1," | |
+ " L5, R4, R2, L1, L4, R1, L1, L1, L5, R3, R4, L2, R1, R2, R1, " | |
+ "R1, R3, L5, R1, R4"; | |
String input1 ="R2, L3"; | |
HashMap<Integer,Integer> xCoord = new HashMap<>(); | |
HashMap<Integer,Integer> yCoord = new HashMap<>(); | |
int x=0,y=0; | |
final int EAST=1,WEST=2,NORTH=3,SOUTH=4; | |
int currDir =NORTH; | |
String[] inputArray = input.split(", "); | |
for(int i=0;i<inputArray.length;i++){ | |
if(inputArray[i].startsWith("R")){ | |
int value = Integer.parseInt(inputArray[i].replace("R","")); | |
switch(currDir){ | |
case WEST: | |
currDir =NORTH; | |
for(int j=y+1;j<=y+value;j++){ | |
if(yCoord.containsKey(j)){ | |
if(x==yCoord.get(j)){ | |
System.out.println("x"+x +"y"+y); | |
break; | |
} | |
} | |
yCoord.put(j,x); | |
} | |
y=y+value; | |
break; | |
case EAST: | |
currDir =SOUTH; | |
for(int j=y-1;j>=y-value;j--){ | |
if(yCoord.containsKey(j)){ | |
if(x==yCoord.get(j)){ | |
System.out.println("x"+x +"y"+y); | |
break; | |
} | |
} | |
yCoord.put(j,x); | |
} | |
y=y-value; | |
break; | |
case NORTH: | |
currDir =EAST; | |
for(int j=x+1;j>=x+value;j++){ | |
if(xCoord.containsKey(j)){ | |
if(y==xCoord.get(j)){ | |
System.out.println("x"+x +"y"+y); | |
break; | |
} | |
} | |
xCoord.put(j,y); | |
} | |
x=x+value; | |
break; | |
case SOUTH: | |
currDir =WEST; | |
for(int j=x-1;j>=x-value;j--){ | |
if(xCoord.containsKey(j)){ | |
if(y==xCoord.get(j)){ | |
System.out.println("x"+x +"y"+y); | |
break; | |
} | |
} | |
xCoord.put(j,y); | |
} | |
x=x-value; | |
break; | |
} | |
}else{ | |
int value = Integer.parseInt(inputArray[i].replace("L","")); | |
switch(currDir){ | |
case WEST: | |
currDir =SOUTH; | |
for(int j=y-1;j>=y-value;j--){ | |
if(yCoord.containsKey(j)){ | |
if(x==yCoord.get(j)){ | |
System.out.println("x"+x +"y"+y); | |
break; | |
} | |
} | |
yCoord.put(j,x); | |
} | |
y=y-value; | |
break; | |
case EAST: | |
currDir =NORTH; | |
for(int j=y+1;j<=y+value;j++){ | |
if(yCoord.containsKey(j)){ | |
if(x==yCoord.get(j)){ | |
System.out.println("x"+x +"y"+y); | |
break; | |
} | |
} | |
yCoord.put(j,x); | |
} | |
y=y+value; | |
break; | |
case NORTH: | |
currDir =WEST; | |
for(int j=x-1;j>=x-value;j--){ | |
if(xCoord.containsKey(j)){ | |
if(y==xCoord.get(j)){ | |
System.out.println("x"+x +"y"+y); | |
break; | |
} | |
} | |
xCoord.put(j,y); | |
} | |
x=x-value; | |
break; | |
case SOUTH: | |
currDir =EAST; | |
for(int j=x+1;j>=x+value;j++){ | |
if(xCoord.containsKey(j)){ | |
if(y==xCoord.get(j)){ | |
System.out.println("x"+x +"y"+y); | |
break; | |
} | |
} | |
xCoord.put(j,y); | |
} | |
x=x+value; | |
break; | |
} | |
} | |
System.out.println("x is"+x +"y is"+y); | |
} | |
System.out.println(x); | |
System.out.println(y); | |
System.out.println(x+y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment