Yachkov

Wild Zoo

Dec 5th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.ComponentModel.Design;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.Linq;
  9. using System.Net.Http.Headers;
  10. using System.Runtime.InteropServices.ComTypes;
  11. using System.Security;
  12. using System.Text;
  13. using System.Transactions;
  14. using System.Xml.Linq;
  15. using System.Text.RegularExpressions;
  16. using System.Threading;
  17.  
  18. namespace Homework
  19. {
  20.     public class Animals
  21.     {
  22.         class AnimalInfo
  23.         {
  24.             public int neededFood { get; set; }
  25.  
  26.             public string area { get; set; }
  27.         }
  28.         class Program
  29.         {
  30.             static void Main()
  31.             {
  32.                 Dictionary<string, AnimalInfo> animals = new Dictionary<string, AnimalInfo>();
  33.                 Dictionary<string, int> areaPopulation = new Dictionary<string, int>();
  34.                 string[] commands = Console.ReadLine().Split(": ");
  35.                 while (commands[0] != "EndDay")
  36.                 {
  37.                     string[] input = commands[1].Split("-");
  38.                     if (commands[0] == "Add")
  39.                     {
  40.                         string animalName = input[0];
  41.                         int neededFood = int.Parse(input[1]);
  42.                         string area = input[2];
  43.  
  44.                         if (!animals.ContainsKey(animalName))
  45.                         {
  46.                             AnimalInfo animal = new AnimalInfo();
  47.                             animal.area = area;
  48.                             animal.neededFood = neededFood;
  49.                             animals[animalName] = animal;
  50.                             if (!areaPopulation.ContainsKey(area))
  51.                             {
  52.                                 areaPopulation[area] = 1;
  53.                             }
  54.                             else
  55.                             {
  56.                                 areaPopulation[area] += 1;
  57.                             }
  58.                         }
  59.                         else
  60.                         {
  61.                             animals[animalName].neededFood += neededFood;
  62.                         }
  63.                     }
  64.                     else if (commands[0] == "Feed")
  65.                     {
  66.                         string animalName = input[0];
  67.                         int feedingWith = int.Parse(input[1]);
  68.                         if (animals.ContainsKey(animalName))
  69.                         {
  70.                             animals[animalName].neededFood -= feedingWith;
  71.                             if (animals[animalName].neededFood <= 0)
  72.                             {
  73.                                 areaPopulation[animals[animalName].area] -= 1;
  74.                                 animals.Remove(animalName);
  75.                                 Console.WriteLine($"{animalName} was successfully fed");
  76.                             }
  77.                         }
  78.                     }
  79.                     commands = Console.ReadLine().Split(": ");
  80.                 }
  81.                 Console.WriteLine("Animals:");
  82.                 foreach (var item in animals.OrderByDescending(a => a.Value.neededFood).ThenBy(a => a.Key))
  83.                 {
  84.                     Console.WriteLine($" {item.Key} -> {item.Value.neededFood}g");
  85.                 }
  86.                 Console.WriteLine("Areas with hungry animals:");
  87.                 foreach (var item in areaPopulation.OrderByDescending(a => a.Value).ThenBy(a => a.Key))
  88.                 {
  89.                     if (item.Value > 0)
  90.                     {
  91.                         Console.WriteLine($" {item.Key}: {item.Value}");
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment