SlavCodes

Move

Sep 8th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import java.util.*;
  2. public class Test3 {
  3.  
  4.  
  5. public static void main(String[] args) {
  6. Scanner reader = new Scanner(System.in);
  7. int startingPoint = Integer.parseInt(reader.nextLine());
  8. String[] arr = reader.nextLine().split(",");
  9. int forwardsSum = 0;
  10. int backwardsSum = 0;
  11. int currentPos=startingPoint;
  12. while (true){
  13. String[] commands = reader.nextLine().split(" ");
  14. String stop = commands[0];
  15. if(stop.equals("exit")){
  16. break;
  17. }
  18. int steps = Integer.parseInt(commands[0]);
  19. String direction = commands[1];
  20. int size = Integer.parseInt(commands[2]);
  21.  
  22. int len = arr.length;
  23. if(direction.equals("forward")){
  24. for (int i = 0; i < steps ; i++) {
  25. currentPos += size;
  26. if(currentPos>len-1){
  27. forwardsSum+= Integer.parseInt(arr[currentPos%len]);
  28. currentPos%=len;
  29. } else {
  30. forwardsSum += Integer.parseInt(arr[currentPos]);
  31. }
  32. }
  33. } else if(direction.equals("backwards")){
  34. for (int i = 0; i <steps ; i++) {
  35. currentPos -= size;
  36. if(currentPos<0){
  37. backwardsSum+= Integer.parseInt(arr[Math.floorMod(currentPos,len)]);
  38. currentPos = Math.floorMod(currentPos,len);
  39. } else {
  40. backwardsSum += Integer.parseInt(arr[currentPos]);
  41. }
  42. }
  43. }
  44. }
  45. System.out.println("Forward: " + forwardsSum);
  46. System.out.println("Backwards: " + backwardsSum);
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment