Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.lang.Math;
  3. import java.util.Scanner;
  4. import java.io.File;
  5. class Robot
  6. {
  7. enum Direction
  8. {
  9. N('N', 0,1),
  10. E('E', 1,0),
  11. S('S', 0,-1),
  12. W('W', -1,0);
  13. public final Point offset;
  14. public final char symbol;
  15. Direction(char symbol, int xOffset, int yOffset)
  16. {
  17. this.symbol = symbol;
  18. this.offset = new Point(xOffset, yOffset);
  19. }
  20. public static Direction rotate(Direction d, char c)
  21. {
  22. if(c=='L')
  23. {
  24. switch(d)
  25. {
  26. case N:
  27. return W;
  28. case E:
  29. return N;
  30. case S:
  31. return E;
  32. case W:
  33. return S;
  34. }
  35. }
  36. else if (c=='R')
  37. {
  38. switch(d)
  39. {
  40. case N:
  41. return E;
  42. case E:
  43. return S;
  44. case S:
  45. return W;
  46. case W:
  47. return N;
  48. }
  49. }
  50. return null;//throw new InvalidSymbolException(c);
  51. }
  52. public static Direction get(char c)
  53. {
  54. for(Direction d:Direction.values())
  55. if(d.symbol==c)
  56. return d;
  57. return null;
  58. }
  59. //class InvalidSymbolException extends Exception
  60. //{
  61. // public InvalidSymbolException(char c)
  62. // {
  63. // this.c=c;
  64. // }
  65. // final char c;
  66. //}
  67. }
  68.  
  69. Point envMax;
  70. Point location;
  71. Direction direction;
  72. Robot(Point envMax, int x, int y, char c)
  73. {
  74. this.envMax=envMax;
  75. location = new Point(x,y);
  76. direction = Direction.get(c);
  77. }
  78. void rotate(char c)
  79. {
  80. direction=Direction.rotate(direction, c);
  81. }
  82. void move()
  83. {
  84. location.translate((int)direction.offset.getX(), (int)direction.offset.getY());
  85. //Presumably we have to clamp/wrap here.
  86. location.setLocation(Math.min(Math.max((int)location.getX(),0),envMax.getX()),Math.min(Math.max((int)location.getY(),0),envMax.getY()));
  87. }
  88. String getPosition()
  89. {
  90. return ""+(int)location.getX()+" "+(int)location.getY()+" "+direction.symbol;
  91. }
  92. public static void main(String[] args)throws Exception
  93. {
  94. Scanner s = new Scanner(new File(args.length>=1?args[0]:"test.txt"));
  95. Point envMax = new Point(s.nextInt(),s.nextInt());
  96. while(s.hasNext())
  97. {
  98. //Create new robot
  99. Robot rb = new Robot(envMax,s.nextInt(),s.nextInt(),s.next().toUpperCase().charAt(0));//Eww java scanner can't grab single char
  100. String path = s.next().toUpperCase();
  101. for(int i=0;i<path.length();i++)
  102. {
  103. char action = path.charAt(i);
  104. if(action=='M')
  105. {
  106. rb.move();
  107. }
  108. else
  109. {
  110. rb.rotate(action);
  111. }
  112. }
  113. System.out.println(rb.getPosition());
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement