Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02._Feed_the_Animals
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var dic = new Dictionary<string, int>();
  12.             var areas = new Dictionary<string, int>();
  13.             string[] input = Console.ReadLine().Split(":");
  14.  
  15.             while (input[0] != "Last Info")
  16.             {
  17.                 string command = input[0];
  18.                 string animalName = input[1];
  19.                 string area = input[3];
  20.  
  21.                 if (command == "Add")
  22.                 {
  23.                     int dailyFoodLimit = int.Parse(input[2]);
  24.                     if (!dic.ContainsKey(animalName))
  25.                     {
  26.                         dic.Add(animalName, dailyFoodLimit);
  27.                         if (!areas.ContainsKey(area))
  28.                         {
  29.                             areas.Add(area, 0);
  30.                         }        
  31.                         areas[area]++;                      
  32.                     }
  33.                     else
  34.                     {
  35.                         dic[animalName] += dailyFoodLimit;
  36.                     }
  37.                 }
  38.                 else
  39.                 {
  40.                     int food = int.Parse(input[2]);
  41.                     if (dic.ContainsKey(animalName))
  42.                     {
  43.                         dic[animalName] -= food;
  44.                         if (dic[animalName] <= 0)
  45.                         {
  46.                             Console.WriteLine($"{dic.FirstOrDefault(x => x.Value == dic[animalName]).Key} was successfully fed");
  47.                             dic.Remove(animalName);
  48.                             areas.Remove(area);
  49.                         }
  50.                     }
  51.                 }
  52.                 input = Console.ReadLine().Split(":");
  53.             }
  54.  
  55.             Console.WriteLine("Animals:");
  56.  
  57.             foreach (var k in dic.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  58.             {
  59.                 Console.WriteLine($"{k.Key} -> {k.Value}g");
  60.             }
  61.  
  62.             Console.WriteLine("Areas with hungry animals:");
  63.  
  64.             foreach (var k in areas.OrderByDescending(x => x.Value))
  65.             {
  66.                 Console.WriteLine($"{k.Key} : {k.Value}");
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement