kaloyan_kolev

Untitled

Jun 17th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel.Design;
  3. using System.Linq;
  4.  
  5. namespace TheGarden
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int rows = int.Parse(Console.ReadLine());
  12.  
  13.             char[][] garden = new char[rows][];
  14.  
  15.             for (int row = 0; row < garden.Length; row++)
  16.             {
  17.                 char[] vegetables = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries)
  18.                     .Select(char.Parse).ToArray();
  19.  
  20.                 garden[row] = vegetables;
  21.             }
  22.  
  23.             int carrotsCount = 0;
  24.             int potatoesCount = 0;
  25.             int lettuceCount = 0;
  26.             int harmedVegetables = 0;
  27.  
  28.             string command = Console.ReadLine();
  29.             while (command != "End of Harvest")
  30.             {
  31.                 string[] tokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  32.  
  33.                 if (tokens.Length == 3)
  34.                 {
  35.                     //Harvest {row} {col}
  36.                     int row = int.Parse(tokens[1]);
  37.                     int col = int.Parse(tokens[2]);
  38.  
  39.                     if (IsInside(garden, row, col))
  40.                     {
  41.                         if (garden[row][col] != ' ')
  42.                         {
  43.                             if (garden[row][col] == 'L')
  44.                             {
  45.                                 lettuceCount += 1;
  46.                             }
  47.                             else if (garden[row][col] == 'P')
  48.                             {
  49.                                 potatoesCount += 1;
  50.                             }
  51.                             else if (garden[row][col] == 'C')
  52.                             {
  53.                                 carrotsCount += 1;
  54.                             }
  55.  
  56.                             garden[row][col] = ' ';
  57.                         }
  58.                     }
  59.                 }
  60.                 else if (tokens.Length == 4)
  61.                 {
  62.                     //Mole {row} {col} {direction}
  63.                     int row = int.Parse(tokens[1]);
  64.                     int col = int.Parse(tokens[2]);
  65.                     string direction = tokens[3].ToLower();
  66.  
  67.                     if (IsInside(garden, row, col))
  68.                     {
  69.                         if (garden[row][col] != ' ')
  70.                         {
  71.                             if (direction == "up")
  72.                             {
  73.                                 for (int currentRow = row; currentRow >= 0; currentRow -=2)
  74.                                 {
  75.                                     if (IsInside(garden, currentRow, col) && garden[currentRow][col] != ' ')
  76.                                     {
  77.                                         garden[currentRow][col] = ' ';
  78.                                         harmedVegetables += 1;
  79.                                     }
  80.                                 }
  81.                             }
  82.                             else if (direction == "down")
  83.                             {
  84.                                 for (int currentRow = row; currentRow < garden.Length; currentRow += 2)
  85.                                 {
  86.                                     if (IsInside(garden, currentRow, col) && garden[currentRow][col] != ' ')
  87.                                     {
  88.                                         garden[currentRow][col] = ' ';
  89.                                         harmedVegetables += 1;
  90.                                     }
  91.                                 }
  92.                             }
  93.                             else if (direction == "left")
  94.                             {
  95.                                 for (int currentCol = col; currentCol >= 0; currentCol -= 2)
  96.                                 {
  97.                                     if (IsInside(garden, row, currentCol) && garden[row][currentCol] != ' ')
  98.                                     {
  99.                                         garden[row][currentCol] = ' ';
  100.                                         harmedVegetables += 1;
  101.                                     }
  102.                                 }
  103.                             }
  104.                             else if (direction == "right")
  105.                             {
  106.                                 for (int currentCol = col; currentCol < garden[row].Length; currentCol += 2)
  107.                                 {
  108.                                     if (IsInside(garden, row, currentCol) && garden[row][currentCol] != ' ')
  109.                                     {
  110.                                         garden[row][currentCol] = ' ';
  111.                                         harmedVegetables += 1;
  112.                                     }
  113.                                 }
  114.                             }
  115.                         }
  116.                     }
  117.                 }
  118.  
  119.                 command = Console.ReadLine();
  120.             }
  121.  
  122.             foreach (char[] row in garden)
  123.             {
  124.                 Console.WriteLine(string.Join(" ", row));
  125.             }
  126.  
  127.             Console.WriteLine($"Carrots: {carrotsCount}");
  128.             Console.WriteLine($"Potatoes: {potatoesCount}");
  129.             Console.WriteLine($"Lettuce: {lettuceCount}");
  130.             Console.WriteLine($"Harmed vegetables: {harmedVegetables}");
  131.         }
  132.  
  133.         static bool IsInside(char[][] jaggedArray, int row, int col)
  134.         {
  135.             return row >= 0 && row < jaggedArray.Length && col >= 0 && col < jaggedArray[row].Length;
  136.         }
  137.     }
  138. }
Add Comment
Please, Sign In to add comment