borovaneca

LadyBugs

Apr 20th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package Fundamentals.Arrays.Exercise.MoreExercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class LadyBugs {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.  
  11.         int[] bugsArr = new int[Integer.parseInt(scanner.nextLine())];
  12.         Arrays.fill(bugsArr, 0);
  13.         int[] indexOfLadyBugs = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  14.         for (int indexes : indexOfLadyBugs) {
  15.             if (indexes < 0 || indexes >= bugsArr.length) {
  16.                 continue;
  17.             } else {
  18.                 bugsArr[indexes] = 1;
  19.             }
  20.         }
  21.  
  22.         String move = scanner.nextLine();
  23.  
  24.         while (!"end".equals(move)) {
  25.             String[] indexesToMove = move.split("\\s+");
  26.             int initialPosition = Integer.parseInt(indexesToMove[0]);
  27.             String direction = indexesToMove[1];
  28.             int countOfMoves = Integer.parseInt(indexesToMove[2]);
  29.  
  30.             int newPosition = 0;
  31.  
  32.             if (countOfMoves < 0)
  33.             {
  34.                 if (direction.equals("left")) {
  35.                     direction = "right";
  36.                     countOfMoves = Math.abs(countOfMoves);
  37.                 }
  38.                 else if (direction.equals("right")) {
  39.                     direction = "left";
  40.                     countOfMoves = Math.abs(countOfMoves);
  41.                 }
  42.             }
  43.  
  44.             if (initialPosition < 0 || initialPosition >= bugsArr.length || bugsArr[initialPosition] == 0) {
  45.                 move = scanner.nextLine();
  46.                 continue;
  47.             } else if (countOfMoves == 0 && initialPosition <= bugsArr.length - 1) {
  48.                 if (bugsArr[initialPosition] == 1) {
  49.                     bugsArr[initialPosition] = 0;
  50.                 }
  51.             }
  52.  
  53.  
  54.             switch (direction) {
  55.                 case "right":
  56.                     newPosition = initialPosition + countOfMoves;
  57.                     bugsArr[initialPosition] = 0;
  58.                     if (newPosition > bugsArr.length - 1) {
  59.                         bugsArr[initialPosition] = 0;
  60.                         break;
  61.                     } else {
  62.                         for (int i = newPosition; i < bugsArr.length; i += countOfMoves) {
  63.                             if (bugsArr[i] == 0) {
  64.                                 bugsArr[i] = 1;
  65.                                 break;
  66.                             }
  67.  
  68.                         }
  69.                     }
  70.                     break;
  71.  
  72.                 case "left":
  73.  
  74.                     newPosition = initialPosition - countOfMoves;
  75.                     bugsArr[initialPosition] = 0;
  76.                     if (newPosition < 0) {
  77.                         bugsArr[initialPosition] = 0;
  78.                         break;
  79.                     } else {
  80.                         for (int i = newPosition; i >= 0; i-= countOfMoves) {
  81.                             if (bugsArr[i] == 0) {
  82.                                 bugsArr[i] = 1;
  83.                                 break;
  84.                             }
  85.                         }
  86.                     }
  87.  
  88.                     break;
  89.             }
  90.  
  91.  
  92.             move = scanner.nextLine();
  93.         }
  94.  
  95.         Arrays.stream(bugsArr).forEach(item -> System.out.print(item + " "));
  96.     }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment