Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _01._The_Garden
- {
- class Program
- {
- static void Main(string[] args)
- {
- int rows = int.Parse(Console.ReadLine());
- char[][] matrix = new char[rows][];
- for (int row = 0; row < rows; row++)
- {
- char[] vegetablas = Console.ReadLine()
- .Split(" ")
- .Select(char.Parse)
- .ToArray();
- matrix[row] = new char[vegetablas.Length];
- for (int col = 0; col < vegetablas.Length; col++)
- {
- matrix[row][col] = vegetablas[col];
- }
- }
- int lettuceCounter = 0;
- int potatoesCounter = 0;
- int carrotsCounter = 0;
- int harmerVegetableCount = 0;
- string input = Console.ReadLine();
- while (input != "End of Harvest")
- {
- string[] splitInput = input.Split();
- string command = splitInput[0];
- int rowCommand = int.Parse(splitInput[1]);
- int colCommand = int.Parse(splitInput[2]);
- if (command == "Harvest")
- {
- if (IsInside(matrix, rowCommand, colCommand))
- {
- if (matrix[rowCommand][colCommand] == 'L')
- {
- lettuceCounter++;
- matrix[rowCommand][colCommand] = ' ';
- }
- else if (matrix[rowCommand][colCommand] == 'P')
- {
- potatoesCounter++;
- matrix[rowCommand][colCommand] = ' ';
- }
- else if (matrix[rowCommand][colCommand] == 'C')
- {
- carrotsCounter++;
- matrix[rowCommand][colCommand] = ' ';
- }
- }
- }
- else if (command == "Mole")
- {
- string directions = splitInput[3];
- if (IsInside(matrix, rowCommand, colCommand))
- {
- if (directions == "up")
- {
- for (int i = rowCommand; i >= 0; i -= 2)
- {
- if (IsInside(matrix, rowCommand, colCommand)
- && matrix[i][colCommand] != ' ')
- {
- matrix[i][colCommand] = ' ';
- harmerVegetableCount++;
- }
- }
- }
- else if (directions == "down")
- {
- for (int i = rowCommand; i < matrix.Length; i += 2)
- {
- if (IsInside(matrix, rowCommand, colCommand)
- && matrix[i][colCommand] != ' ')
- {
- matrix[i][colCommand] = ' ';
- harmerVegetableCount++;
- }
- }
- }
- else if (directions == "left")
- {
- for (int currentCol = colCommand; currentCol >= 0; currentCol -= 2)
- {
- if (IsInside(matrix, rowCommand, colCommand)
- && matrix[rowCommand][currentCol] != ' ')
- {
- matrix[rowCommand][currentCol] = ' ';
- harmerVegetableCount++;
- }
- }
- }
- else if (directions == "right")
- {
- for (int currenCol = colCommand; currenCol < matrix[rowCommand].Length; currenCol += 2)
- {
- if (IsInside(matrix, rowCommand, colCommand)
- && matrix[rowCommand][currenCol] != ' ')
- {
- matrix[rowCommand][currenCol] = ' ';
- harmerVegetableCount++;
- }
- }
- }
- }
- }
- input = Console.ReadLine();
- }
- foreach (var garden in matrix)
- {
- Console.WriteLine(String.Join(" ",garden).TrimEnd());
- }
- Console.WriteLine($"Carrots: {carrotsCounter}");
- Console.WriteLine($"Potatoes: {potatoesCounter}");
- Console.WriteLine($"Lettuce: {lettuceCounter}");
- Console.WriteLine($"Harmed vegetables: {harmerVegetableCount}");
- }
- static bool IsInside(char[][] matrix, int rowCommand, int colCommand)
- {
- return rowCommand >= 0 && rowCommand < matrix.Length &&
- colCommand >= 0 && colCommand < matrix[rowCommand].Length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement