Advertisement
ivo_petkov01

Santa's New List

Mar 30th, 2023
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04._Santa_s_New_List_2
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string commandLine = Console.ReadLine();
  12.  
  13.             Dictionary<string, int> children = new Dictionary<string, int>();
  14.             Dictionary<string, int> toys = new Dictionary<string, int>();
  15.  
  16.             while (commandLine != "END")
  17.             {
  18.                 string[] cmdArgs = commandLine.Split("->", StringSplitOptions.RemoveEmptyEntries);
  19.                 string command = cmdArgs.First();
  20.  
  21.                 if (command != "Remove")
  22.                 {
  23.                     string name = command;
  24.                     string typeOfToy = cmdArgs[1];
  25.                     int amount = int.Parse(cmdArgs.Last());
  26.  
  27.                     if (!children.ContainsKey(name))
  28.                     {
  29.                         children.Add(name, amount);
  30.                     }
  31.                     else
  32.                     {
  33.                         children[name] += amount;
  34.                     }
  35.  
  36.                     if (!toys.ContainsKey(typeOfToy))
  37.                     {
  38.                         toys.Add(typeOfToy, amount);
  39.                     }
  40.                     else
  41.                     {
  42.                         toys[typeOfToy] += amount;
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     string nameToRemove = cmdArgs.Last();
  48.  
  49.                     if (children.ContainsKey(nameToRemove))
  50.                     {
  51.                         children.Remove(nameToRemove);
  52.                     }
  53.                 }
  54.  
  55.                 commandLine = Console.ReadLine();
  56.             }
  57.  
  58.             Console.WriteLine("Children:");
  59.  
  60.             foreach (KeyValuePair<string, int> kvp in children.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  61.             {
  62.                 Console.WriteLine($"{kvp.Key} -> {kvp.Value}");
  63.             }
  64.  
  65.             Console.WriteLine("Presents:");
  66.  
  67.             foreach (KeyValuePair<string, int> kvp in toys)
  68.             {
  69.                 Console.WriteLine($"{kvp.Key} -> {kvp.Value}");
  70.             }
  71.         }
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement