Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Test3 {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- int startingPoint = Integer.parseInt(reader.nextLine());
- String[] arr = reader.nextLine().split(",");
- int forwardsSum = 0;
- int backwardsSum = 0;
- int currentPos=startingPoint;
- while (true){
- String[] commands = reader.nextLine().split(" ");
- String stop = commands[0];
- if(stop.equals("exit")){
- break;
- }
- int steps = Integer.parseInt(commands[0]);
- String direction = commands[1];
- int size = Integer.parseInt(commands[2]);
- int len = arr.length;
- if(direction.equals("forward")){
- for (int i = 0; i < steps ; i++) {
- currentPos += size;
- if(currentPos>len-1){
- forwardsSum+= Integer.parseInt(arr[currentPos%len]);
- currentPos%=len;
- } else {
- forwardsSum += Integer.parseInt(arr[currentPos]);
- }
- }
- } else if(direction.equals("backwards")){
- for (int i = 0; i <steps ; i++) {
- currentPos -= size;
- if(currentPos<0){
- backwardsSum+= Integer.parseInt(arr[Math.floorMod(currentPos,len)]);
- currentPos = Math.floorMod(currentPos,len);
- } else {
- backwardsSum += Integer.parseInt(arr[currentPos]);
- }
- }
- }
- }
- System.out.println("Forward: " + forwardsSum);
- System.out.println("Backwards: " + backwardsSum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment