Didart

LadyBugs

Oct 5th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class LadyBugs {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int fieldSize = Integer.parseInt(scanner.nextLine());
  9.  
  10.         int[] field = new int[fieldSize];
  11.  
  12.         int[] ladyBugsIndexes = Arrays.stream(scanner.nextLine()
  13.                 .split(" "))
  14.                 .mapToInt(Integer::parseInt)
  15.                 .toArray();
  16.         for (int ladyBugIndex : ladyBugsIndexes) {
  17.             if (ladyBugIndex >= 0 && ladyBugIndex <= field.length - 1) {
  18.                 field[ladyBugIndex] = 1;
  19.             }
  20.         }
  21.        
  22.         String command = scanner.nextLine();
  23.         while (!command.equals("end")) {
  24.             String[] tokens = command.split(" ");
  25.             int index = Integer.parseInt(tokens[0]);
  26.             String direction = tokens[1];
  27.             int flyLength = Integer.parseInt(tokens[2]);
  28.  
  29.             if (index >= 0 && index <= field.length - 1 && field[index] == 1) {
  30.                 field[index] = 0;
  31.                 if (direction.equals("right")) {
  32.                     index += flyLength;
  33.  
  34.                     while (index <= field.length - 1 && field[index] == 1) {
  35.                         index += flyLength;
  36.  
  37.                     }
  38.                     if (index <= field.length - 1) {
  39.                         field[index] = 1;
  40.                     }
  41.                 } else if (direction.equals("left")) {
  42.                     index -= flyLength;
  43.                     while (index >= 0 && field[index] == 1) {
  44.                         index -= flyLength;
  45.                     }
  46.                     if (index >= 0) {
  47.                         field[index] = 1;
  48.                     }
  49.                 }
  50.             }
  51.             command = scanner.nextLine();
  52.         }
  53.         for (int number : field) {
  54.             System.out.print(number + " ");
  55.         }
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment