Advertisement
SophiYo

LadyBug

Feb 17th, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. package O3Arrays.Exercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Ex10LadyFuckingNew {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int  fieldSize  = Integer.parseInt(scanner.nextLine());
  11.  
  12.         int[] field = new int[fieldSize];
  13.  
  14.         String[] initialBugs = Arrays.stream(scanner.nextLine().split("[\\s]"))
  15.                 .filter(e-> !e.equals(""))
  16.                 .map(String::toString)
  17.                 .toArray(String[]::new);
  18.  
  19.  
  20.         for (String initialBug : initialBugs) {
  21.             int index = Integer.parseInt(initialBug);
  22.             if (index >= 0 && index < fieldSize) {
  23.                 field[index] = 1;
  24.             }
  25.         }
  26.  
  27.         String command = scanner.nextLine() ;
  28.  
  29.         while (!"end".equals(command)) {
  30.  
  31.             String[] actionCommand = command.split("[\\s]");
  32.  
  33.             int bugToMove = Integer.parseInt(actionCommand[0]);
  34.             String direction = actionCommand[1];
  35.             int flyLength = Integer.parseInt(actionCommand[2]);
  36.  
  37.             if (bugToMove < 0 || bugToMove > fieldSize - 1 || field[bugToMove] == 0) {
  38.                 command = scanner.nextLine() ;
  39.                 continue;
  40.  
  41.             } else {
  42.                 field[bugToMove] = 0;
  43.  
  44.                 if ("right".equals(direction) ) {
  45.                     bugToMove += flyLength;
  46.  
  47.                     while (bugToMove < fieldSize && field[bugToMove]==1) {
  48.                         bugToMove += flyLength;
  49.                     }
  50.                     if (bugToMove < fieldSize) {
  51.  
  52.                             field[bugToMove] = 1;
  53.  
  54.  
  55.                     }
  56.                 } else {
  57.                     bugToMove -= flyLength;
  58.  
  59.                     while (bugToMove >= 0 && field[bugToMove]==1) {
  60.                         bugToMove -= flyLength;
  61.                     }
  62.                     if (bugToMove >= 0) {
  63.                         if (bugToMove<fieldSize) {
  64.                             field[bugToMove] = 1;
  65.                         }
  66.  
  67.  
  68.                     }
  69.                 }
  70.             }
  71.             command = scanner.nextLine() ;
  72.         }
  73.         for (int i1 : field) {
  74.             System.out.print(i1 + " ");
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement