Created
August 18, 2017 16:14
-
-
Save Robadob/f9cdad68c8b388bc99fcd420f88b8dca 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.awt.Point; | |
import java.lang.Math; | |
import java.util.Scanner; | |
import java.io.File; | |
class Robot | |
{ | |
enum Direction | |
{ | |
N('N', 0,1), | |
E('E', 1,0), | |
S('S', 0,-1), | |
W('W', -1,0); | |
public final Point offset; | |
public final char symbol; | |
Direction(char symbol, int xOffset, int yOffset) | |
{ | |
this.symbol = symbol; | |
this.offset = new Point(xOffset, yOffset); | |
} | |
public static Direction rotate(Direction d, char c) | |
{ | |
if(c=='L') | |
{ | |
switch(d) | |
{ | |
case N: | |
return W; | |
case E: | |
return N; | |
case S: | |
return E; | |
case W: | |
return S; | |
} | |
} | |
else if (c=='R') | |
{ | |
switch(d) | |
{ | |
case N: | |
return E; | |
case E: | |
return S; | |
case S: | |
return W; | |
case W: | |
return N; | |
} | |
} | |
return null;//throw new InvalidSymbolException(c); | |
} | |
public static Direction get(char c) | |
{ | |
for(Direction d:Direction.values()) | |
if(d.symbol==c) | |
return d; | |
return null; | |
} | |
//class InvalidSymbolException extends Exception | |
//{ | |
// public InvalidSymbolException(char c) | |
// { | |
// this.c=c; | |
// } | |
// final char c; | |
//} | |
} | |
Point envMax; | |
Point location; | |
Direction direction; | |
Robot(Point envMax, int x, int y, char c) | |
{ | |
this.envMax=envMax; | |
location = new Point(x,y); | |
direction = Direction.get(c); | |
} | |
void rotate(char c) | |
{ | |
direction=Direction.rotate(direction, c); | |
} | |
void move() | |
{ | |
location.translate((int)direction.offset.getX(), (int)direction.offset.getY()); | |
//Presumably we have to clamp/wrap here. | |
location.setLocation(Math.min(Math.max((int)location.getX(),0),envMax.getX()),Math.min(Math.max((int)location.getY(),0),envMax.getY())); | |
} | |
String getPosition() | |
{ | |
return ""+(int)location.getX()+" "+(int)location.getY()+" "+direction.symbol; | |
} | |
public static void main(String[] args)throws Exception | |
{ | |
Scanner s = new Scanner(new File(args.length>=1?args[0]:"test.txt")); | |
Point envMax = new Point(s.nextInt(),s.nextInt()); | |
while(s.hasNext()) | |
{ | |
//Create new robot | |
Robot rb = new Robot(envMax,s.nextInt(),s.nextInt(),s.next().toUpperCase().charAt(0));//Eww java scanner can't grab single char | |
String path = s.next().toUpperCase(); | |
for(int i=0;i<path.length();i++) | |
{ | |
char action = path.charAt(i); | |
if(action=='M') | |
{ | |
rb.move(); | |
} | |
else | |
{ | |
rb.rotate(action); | |
} | |
} | |
System.out.println(rb.getPosition()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment