TheBulgarianWolf

Ladybugs Exercise

Oct 18th, 2020
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Numerics;
  6.  
  7. namespace SoftuniExercisesWithVariables
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             Console.WriteLine("Enter the size of the field: ");
  15.             int fieldSize = int.Parse(Console.ReadLine());
  16.             int[] field = new int[fieldSize];
  17.            
  18.             Console.WriteLine("Enter the indexes of the ladybugs: ");
  19.             int[] initialIndexes = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  20.             for(int i = 0; i < initialIndexes.Length; i++)
  21.             {
  22.                 //setting ladybugs inside the field
  23.                 field[initialIndexes[i]] = 1;
  24.             }
  25.             string input = "";
  26.             while((input = Console.ReadLine()) != "end")
  27.             {
  28.                 string[] array = input.Split(" ");
  29.                 int index = int.Parse(array[0]);
  30.                 string direction = array[1];
  31.                 int moves = int.Parse(array[2]);
  32.                 if(direction == "left" && moves < 0)
  33.                 {
  34.                     moves = -moves;
  35.                 }
  36.                 else if(direction == "left" && moves > 0)
  37.                 {
  38.                     moves = -moves;
  39.                 }
  40.  
  41.  
  42.                 if(index<0 || index >= fieldSize)
  43.                 {
  44.                     continue;
  45.                 }
  46.                 else if(fieldSize <= index + moves || index + moves < 0)
  47.                 {
  48.                     field[index] = 0;
  49.                 }
  50.                 else
  51.                 {
  52.                     if(field[index+moves] == 0)
  53.                     {
  54.                         field[index + moves] = 1;
  55.                         field[index] = 0;
  56.                     }
  57.                     else
  58.                     {
  59.                         field[index] = 0;
  60.                         while (field[index + moves] != 0)
  61.                         {
  62.                             moves++;
  63.                             if(field[index+moves] == 0)
  64.                             {
  65.                                 field[index + moves] = 1;
  66.                                 break;
  67.                             }
  68.                         }
  69.                     }
  70.                    
  71.                 }
  72.                
  73.             }
  74.             for(int k = 0; k < fieldSize; k++)
  75.             {
  76.                 Console.Write(field[k] + " ");
  77.             }
  78.         }
  79.     }
  80. }
  81.  
Add Comment
Please, Sign In to add comment