Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Heroes
- {
- using System;
- using System.Linq;
- using System.Collections.Generic;
- public class StartUp
- {
- public static int harmed;
- public static int lettuce;
- public static int potatos;
- public static int carrots;
- public static int moleRow;
- public static int moleCol;
- public static int harvestRow;
- public static int harvestCol;
- public static char[][] garden;
- public static string direction;
- public static int size;
- public static void Main()
- {
- ReadInput();
- }
- private static void ReadInput()
- {
- size = int.Parse(Console.ReadLine());
- harmed = 0;
- garden = new char[size][];
- InitializeMatrix();
- string input;
- while ((input = Console.ReadLine()) != "End of Harvest")
- {
- string[] tokens = input.Split();
- string command = tokens[0];
- if (command == "Harvest")
- {
- harvestRow = int.Parse(tokens[1]);
- harvestCol = int.Parse(tokens[2]);
- if (IsInside())
- {
- if (garden[harvestRow][harvestCol] != ' ')
- {
- char vegetable = garden[harvestRow][harvestCol];
- HarvestVegetable(vegetable);
- }
- garden[harvestRow][harvestCol] = ' ';
- }
- }
- else if (command == "Mole")
- {
- moleRow = int.Parse(tokens[1]);
- moleCol = int.Parse(tokens[2]);
- direction = tokens[3].ToLower();
- if (IsMoleInside())
- {
- MoveMole();
- }
- }
- }
- Print();
- Console.WriteLine($"Carrots: {carrots}");
- Console.WriteLine($"Potatoes: {potatos}");
- Console.WriteLine($"Lettuce: {lettuce}");
- Console.WriteLine($"Harmed vegetables: {harmed}");
- }
- private static void MoveMole()
- {
- if (direction == "up")
- {
- for (int row = moleRow; row >= 0; row -= 2)
- {
- if (NotHarvestedOrHarmed(row, moleCol))
- {
- harmed++;
- garden[row][moleCol] = ' ';
- }
- }
- }
- else if (direction == "right")
- {
- for (int col = moleCol; col < garden[moleRow].Length; col += 2)
- {
- if (NotHarvestedOrHarmed(moleRow, col))
- {
- harmed++;
- garden[moleRow][col] = ' ';
- }
- }
- }
- else if (direction == "down")
- {
- for (int row = moleRow; row < size; row += 2)
- {
- if (NotHarvestedOrHarmed(row, moleCol))
- {
- harmed++;
- garden[row][moleCol] = ' ';
- }
- }
- }
- else if (direction == "left")
- {
- for (int col = moleCol; col >= 0; col -= 2)
- {
- if (NotHarvestedOrHarmed(moleRow, col))
- {
- harmed++;
- garden[moleRow][col] = ' ';
- }
- }
- }
- }
- private static bool NotHarvestedOrHarmed(int row, int col)
- {
- return garden[row][col] != ' ';
- }
- private static void HarvestVegetable(char vegetable)
- {
- switch (vegetable)
- {
- case 'L': lettuce++; break;
- case 'P': potatos++; break;
- case 'C': carrots++; break;
- }
- }
- private static bool IsInside()
- {
- return harvestRow >= 0 && harvestRow < garden.Length && harvestCol >= 0 && harvestCol < garden[harvestRow].Length;
- }
- private static bool IsMoleInside()
- {
- return moleRow >= 0 && moleRow < garden.Length && moleCol >= 0 && moleCol < garden[moleRow].Length;
- }
- private static void InitializeMatrix()
- {
- for (int row = 0; row < size; row++)
- {
- garden[row] = Console.ReadLine().ToCharArray();
- garden[row] = garden[row].Where(x => x != ' ').ToArray();
- }
- }
- private static void Print()
- {
- foreach (var row in garden)
- {
- Console.WriteLine(string.Join(" ", row));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement