Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class ladybugs {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int fieldSize = Integer.parseInt(scanner.nextLine());
- int[] field = new int[fieldSize];
- int[] bugs = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(e -> Integer.parseInt(e)).toArray();
- for (int ifBug : bugs) {
- field[ifBug] = 1;
- }
- outter:
- while (true) {
- String input = scanner.nextLine();
- if ("end".equals(input)) {
- break;
- }
- String[] commands = input.split(" ");
- int position = Integer.parseInt(commands[0]);
- String direction = commands[1];
- int flyLenght = Integer.parseInt(commands[2]);
- // If you are given ladybug index that is outside the field
- if (position < 0 || position >= field.length) {
- continue;
- }
- //If you are given ladybug index that does not have ladybug there
- if (field[position] == 0) {
- continue;
- //empty the index after it has flew away
- } else {
- field[position] = 0;
- }
- if (direction.equals("right")) {
- //if it lands somewhere outside of the field
- if (position + flyLenght >= field.length) {
- continue;
- }
- //if landing index is free
- if (field[position + flyLenght] == 0) {
- field[position + flyLenght] = 1;
- //if landing index is occupied
- } else {
- int landingIndex = position + flyLenght;
- while (field[landingIndex] == 1) {
- landingIndex += flyLenght;
- if (landingIndex >= field.length) {
- continue outter;
- }
- }
- field[landingIndex] = 1;
- }
- } else if (direction.equals("left")) {
- //if it lands somewhere outside of the field
- if (position - flyLenght < 0) {
- continue;
- }
- //if landing index is free
- if (field[position - flyLenght] == 0) {
- field[position - flyLenght] = 1;
- //if landing index is occupied
- } else {
- int landingIndex = position - flyLenght;
- while (field[landingIndex] == 1) {
- landingIndex -= flyLenght;
- if (landingIndex < 0) {
- continue outter;
- }
- }
- field[landingIndex] = 1;
- }
- }
- }
- for (int print : field) {
- System.out.print(print + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement