Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _01.TheGarden
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<List<string>> garden = new List<List<string>>();
- List<int> harmedVegetables = new List<int>();
- Dictionary<string, int> dictVegetables = new Dictionary<string, int>();
- AddCaindOfVegetableInDict(dictVegetables);
- int numberRolInGarden = int.Parse(Console.ReadLine());
- for (int i = 0; i < numberRolInGarden; i++)
- {
- List<string> lineInGarden = CreateLine(Console.ReadLine());
- garden.Add(lineInGarden);
- }
- string command;
- while ((command = Console.ReadLine()) != "End of Harvest")
- {
- Queue<string> qCommand = new Queue<string>(command.Split(" ", StringSplitOptions.RemoveEmptyEntries));
- string firstCommand = qCommand.Dequeue();
- switch (firstCommand)
- {
- case "Harvest":
- HarvestFromGarden(qCommand, garden, dictVegetables);
- break;
- case "Mole":
- MoleAttackGarden(qCommand, garden, harmedVegetables);
- break;
- }
- }
- PrintGarden(garden);
- PrintVegDict(dictVegetables);
- Console.WriteLine($"Harmed vegetables: {harmedVegetables.Count}");
- }
- private static void MoleAttackGarden(Queue<string> qCommand, List<List<string>> garden, List<int> harmedVegetables)
- {
- int moleRow = int.Parse(qCommand.Dequeue());
- int moleCol = int.Parse(qCommand.Dequeue());
- string direction = qCommand.Dequeue().ToLower();
- if ((garden.Count - 1 >= moleRow) && (garden[moleRow].Count - 1 >= moleCol) && garden[moleRow][moleCol] != " ")
- {
- harmedVegetables.Add(1);
- garden[moleRow][moleCol] = " ";
- switch (direction)
- {
- case "up":
- moleRow -= 2;
- while (moleRow >= 0)
- {
- if ((garden.Count - 1 >= moleRow)
- && (garden[moleRow].Count - 1 >= moleCol)
- && garden[moleRow][moleCol] != " ")
- {
- harmedVegetables.Add(1);
- garden[moleRow][moleCol] = " ";
- }
- moleRow -= 2;
- }
- break;
- case "down":
- moleRow += 2;
- while (garden.Count-1 >= moleRow)
- {
- if ((garden.Count - 1 >= moleRow)
- && (garden[moleRow].Count - 1 >= moleCol)
- && garden[moleRow][moleCol] != " ")
- {
- harmedVegetables.Add(1);
- garden[moleRow][moleCol] = " ";
- }
- moleRow += 2;
- }
- break;
- case "right":
- moleCol += 2;
- while (garden[moleRow].Count-1 >= moleCol)
- {
- if ((garden.Count - 1 >= moleRow)
- && (garden[moleRow].Count - 1 >= moleCol)
- && garden[moleRow][moleCol] != " ")
- {
- harmedVegetables.Add(1);
- garden[moleRow][moleCol] = " ";
- }
- moleCol += 2;
- }
- break;
- case "left":
- moleCol -= 2;
- while (moleCol>=0)
- {
- if ((garden.Count - 1 >= moleRow)
- && (garden[moleRow].Count - 1 >= moleCol)
- && garden[moleRow][moleCol] != " ")
- {
- harmedVegetables.Add(1);
- garden[moleRow][moleCol] = " ";
- }
- moleCol -= 2;
- }
- break;
- }
- }
- }
- private static void PrintVegDict(Dictionary<string, int> dictVegetables)
- {
- foreach (var vege in dictVegetables)
- {
- if (vege.Key == "C")
- {
- Console.WriteLine($"Carrots: {vege.Value}");
- }
- else if (vege.Key == "P")
- {
- Console.WriteLine($"Potatoes: {vege.Value}");
- }
- else
- {
- Console.WriteLine($"Lettuce: {vege.Value}");
- }
- }
- }
- private static void HarvestFromGarden(Queue<string> qCommand, List<List<string>> garden, Dictionary<string, int> dictVegetables)
- {
- int rowHarvest = int.Parse(qCommand.Dequeue());
- int colHarvest = int.Parse(qCommand.Dequeue());
- if ((garden.Count - 1 >= rowHarvest) && (garden[rowHarvest].Count - 1 >= colHarvest) && garden[rowHarvest][colHarvest] != " ")
- {
- string vegiKey = garden[rowHarvest][colHarvest];
- garden[rowHarvest][colHarvest] = " ";
- dictVegetables[vegiKey]++;
- }
- }
- private static void AddCaindOfVegetableInDict(Dictionary<string, int> dictVegetables)
- {
- dictVegetables.Add("C", 0);
- dictVegetables.Add("P", 0);
- dictVegetables.Add("L", 0);
- }
- private static void PrintGarden(List<List<string>> garden)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- foreach (var gardenLine in garden)
- {
- Console.WriteLine(string.Join(" ", gardenLine));
- }
- Console.ForegroundColor = ConsoleColor.White;
- }
- private static List<string> CreateLine(string input)
- {
- var result = input.Split(" ", StringSplitOptions.RemoveEmptyEntries).
- Select(x=>x.ToUpper()).
- ToList();
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement