Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _01.Template
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //keeps the key materials
  12.             var keyMat = new Dictionary<string, int>()
  13.             {
  14.                 {"fragments", 0},
  15.                 {"motes", 0},
  16.                 {"shards", 0}
  17.             };
  18.             //keeps the junk materials
  19.             var junkMat = new SortedDictionary<string, int>();
  20.  
  21.             //bool to check if we found a legendary. Finds use later to break out of the while loop.
  22.             bool isFound = false;
  23.  
  24.             //we're getting the farming goods line by line, we're stopping when we
  25.             //obtain a legendary.
  26.             while (true)
  27.             {
  28.                 string mainInput = Console.ReadLine().ToLower();
  29.  
  30.                 //since the input comes as a big wall of text, containing the
  31.                 //materials and their quantity, we have to split it in order to
  32.                 //populate a Dictionary:
  33.  
  34.                 //arr which will split the mainInput and fill the list I'm about
  35.                 //to declare.
  36.                 string[] arr = mainInput.Split(" ");
  37.  
  38.                 List<string> list = new List<string>();
  39.  
  40.  
  41.                 //index steps += 2, because the input is given as <material><quantity><material><quantity> etc..
  42.                 for (int i = 0; i < arr.Length; i += 2)
  43.                 {
  44.                     list.Add(arr[i] + " " + arr[i + 1]);
  45.                 }
  46.  
  47.  
  48.                 for (int farming = 0; farming < list.Count; farming++)
  49.                 {
  50.                     string[] farmed = list[farming].Split(" ").ToArray();
  51.                     string material = farmed[1];
  52.                     int quantity = int.Parse(farmed[0]);
  53.  
  54.                     if (material.Equals("motes") || material.Equals("fragments")
  55.                                                  || material.Equals("shards"))
  56.                     {
  57.                         if (!keyMat.ContainsKey(material))
  58.                         {
  59.                             keyMat.Add(material, quantity);
  60.                         }
  61.                         else
  62.                         {
  63.                             keyMat[material] += quantity;
  64.                         }
  65.  
  66.                         if (keyMat[material] >= 250)
  67.                         {
  68.                             //Check what material contains more than 250 elements and printing the obtained
  69.                             //legendary, reducing the material quantity after obtaining the item.
  70.                             if (material.Equals("motes"))
  71.                             {
  72.                                 Console.WriteLine("Dragonwrath obtained!");
  73.                             }
  74.                             else if (material.Equals("shards"))
  75.                             {
  76.                                 Console.WriteLine("Shadowmourne obtained!");
  77.                             }
  78.                             else if (material.Equals("fragments"))
  79.                             {
  80.                                 Console.WriteLine("Valanyr obtained!");
  81.                             }
  82.  
  83.                             keyMat[material] -= 250;
  84.                             isFound = true;
  85.                             break;
  86.                         }
  87.                     }
  88.                     else
  89.                     {
  90.                         if (!junkMat.ContainsKey(material))
  91.                         {
  92.                             junkMat.Add(material, quantity);
  93.                         }
  94.                         else
  95.                         {
  96.                             junkMat[material] += quantity;
  97.                         }
  98.                     }
  99.                 }
  100.  
  101.                 if (isFound)
  102.                 {
  103.                     break;
  104.                 }
  105.             }
  106.  
  107.             //order by descending order by quantity, if two keys quantity equal -> order alphabetically
  108.             foreach (var item in keyMat.OrderByDescending(x => x.Value).ThenBy(y => y.Key))
  109.             {
  110.                 string material = item.Key;
  111.                 int quantity = item.Value;
  112.  
  113.                 Console.WriteLine($"{material}: {quantity}");
  114.             }
  115.  
  116.             foreach (var item in junkMat)
  117.             {
  118.                 string material = item.Key;
  119.                 int quantity = item.Value;
  120.  
  121.                 Console.WriteLine($"{material}: {quantity}");
  122.             }
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement