Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace Garden
- {
- public class Garden
- {
- public static void Main(string[] args)
- {
- int rowsCount = int.Parse(Console.ReadLine());
- char[][] garden = new char[rowsCount][];
- int carrots = 0;
- int lettuce = 0;
- int potatos = 0;
- int totalHarmed = 0;
- for (int i = 0; i < rowsCount; i++)
- {
- char[] rowVegetables = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(v => char.Parse(v)).ToArray();
- garden[i] = rowVegetables;
- }
- string command = Console.ReadLine();
- while (command.ToLower() != "end of harvest")
- {
- string[] commandParts = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
- int row = int.Parse(commandParts[1]);
- int col = int.Parse(commandParts[2]);
- if (commandParts[0].ToLower() == "harvest")
- {
- if (IsCellValid(garden, row, col))
- {
- if (garden[row][col].Equals('L'))
- lettuce++;
- if (garden[row][col].Equals('C'))
- carrots++;
- if (garden[row][col].Equals('P'))
- potatos++;
- garden[row][col] = ' ';
- }
- }
- if (commandParts[0].ToLower() == "mole")
- {
- totalHarmed += Mole(garden, commandParts, row, col);
- }
- command = Console.ReadLine();
- }
- PrintGarden(garden, carrots, lettuce, potatos, totalHarmed);
- }
- private static int Mole(char[][] garden, string[] commandParts, int row, int col)
- {
- var harmed = 0;
- string dir = commandParts[3].ToLower();
- if (IsCellValid(garden, row, col))
- {
- garden[row][col] = ' ';
- harmed++;
- switch (dir)
- {
- case "up":
- while (IsCellValid(garden, row - 2, col))
- {
- row -= 2;
- garden[row][col] = ' ';
- harmed++;
- }
- break;
- case "down":
- while (IsCellValid(garden, row + 2, col))
- {
- row += 2;
- garden[row][col] = ' ';
- harmed++;
- }
- break;
- case "left":
- while (IsCellValid(garden, row, col - 2))
- {
- col -= 2;
- garden[row][col] = ' ';
- harmed++;
- }
- break;
- case "right":
- while (IsCellValid(garden, row, col + 2))
- {
- col += 2;
- garden[row][col] = ' ';
- harmed++;
- }
- break;
- }
- }
- return harmed;
- }
- private static bool IsCellValid(char[][] garden, int row, int col)
- {
- if ((row >= 0 && row < garden.Length && col >= 0 && col < garden[row].Length) && garden[row][col] != ' ')
- {
- return true;
- }
- return false;
- }
- private static void PrintGarden(char[][] garden, int carrots, int lettuce, int potatoes, int harmed)
- {
- for (int row = 0; row < garden.Length; row++)
- {
- Console.WriteLine(string.Join(" ", garden[row]));
- }
- Console.WriteLine("Carrots: " + carrots);
- Console.WriteLine("Potatoes: " + potatoes);
- Console.WriteLine("Lettuce: " + lettuce);
- Console.WriteLine("Harmed vegetables: " + harmed);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement