JulianJulianov

08.AssociativeArray - Legendary Farming

Mar 14th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.12 KB | None | 0 0
  1. 08.Legendary Farming
  2. You’ve done all the work and the last thing left to accomplish is to own a legendary item. However, it’s a tedious process and it requires quite a bit of farming. Anyway, you are not too pretentious – any legendary item will do. The possible items are:
  3. • Shadowmourne – requires 250 Shards;
  4. • Valanyr – requires 250 Fragments;
  5. • Dragonwrath – requires 250 Motes;
  6. Shards, Fragments and Motes are the key materials and everything else is junk. You will be given lines of input, in the format:
  7. 2 motes 3 ores 15 stones
  8. Keep track of the key materials - the first one that reaches the 250 mark, wins the race. At that point you have to print that the corresponding legendary item is obtained. Then, print the remaining shards, fragments, motes, ordered by quantity in descending order, then by name in ascending order, each on a new line. Finally, print the collected junk items in alphabetical order.
  9. Input
  10. • Each line comes in the following format: {quantity} {material} {quantity} {material}{quantity} {material}
  11. Output
  12. • On the first line, print the obtained item in the format: {Legendary item} obtained!
  13. • On the next three lines, print the remaining key materials in descending order by quantity
  14. o   If two key materials have the same quantity, print them in alphabetical order
  15. • On the final several lines, print the junk items in alphabetical order
  16. o   All materials are printed in format {material}: {quantity}
  17. o   The output should be lowercase, except for the first letter of the legendary
  18. Examples
  19. Input                                                        Output
  20. 3 Motes 5 stones 5 Shards                                    Valanyr obtained!
  21. 6 leathers 255 fragments 7 Shards                            fragments: 5
  22.                                                              shards: 5
  23.                                                              motes: 3
  24.                                                              leathers: 6
  25.                                                              stones: 5
  26.  
  27. 123 silver 6 shards 8 shards 5 motes                         Dragonwrath obtained!
  28. 9 fangs 75 motes 103 MOTES 8 Shards                          shards: 22
  29. 86 Motes 7 stones 19 silver                                  motes: 19
  30.                                                              fragments: 0
  31.                                                              fangs: 9
  32.                                                              silver: 123
  33.  
  34.  
  35. using System;
  36. using System.Collections.Generic;
  37. using System.Linq;
  38.  
  39. namespace _03LegendaryFarming
  40. {
  41.     class Program
  42.     {
  43.         static void Main(string[] args)
  44.         {
  45.             var materials = new Dictionary<string, int>();
  46.             var legendaryItem = new[] { "shards", "fragments", "motes" };
  47.             materials.Add(legendaryItem[0], 0);
  48.             materials.Add(legendaryItem[1], 0);
  49.             materials.Add(legendaryItem[2], 0);
  50.  
  51.             var junk = new Dictionary<string, int>();
  52.             var quantity = 0;
  53.             var name = "";
  54.  
  55.             while (materials["shards"] < 250 && materials["fragments"] < 250 && materials["motes"] < 250)
  56.             {
  57.                 var input = Console.ReadLine().ToLower().Split();
  58.               /*for (int i = 1; i <= input.Length; i += 2)// По-кратко решение!
  59.                 {
  60.                     quantity = int.Parse(input[i]);
  61.                     var material = input[i + 1];
  62.  
  63.                     switch (material)
  64.                     {
  65.                         case "shards":
  66.                         case "fragments":
  67.                         case "motes":
  68.                             materials[material] += quantity;
  69.                             break;
  70.                         default:
  71.                             if (!junk.ContainsKey(material))
  72.                                 junk.Add(material, 0);
  73.                                 junk[material] += quantity;
  74.                                 break;
  75.                      }
  76.                      if (materials["shards"] >= 250 || materials["fragments"] >= 250 || materials["motes"] >= 250)
  77.                          break;
  78.                     }
  79.                     if (materials["shards"] >= 250)
  80.                     {
  81.                        name = "Shadowmourne";
  82.                        materials["shards"] -= 250;
  83.                     }
  84.                     else if (materials["fragments"] >= 250)
  85.                     {
  86.                        name = "Valanyr";
  87.                        materials["fragments"] -= 250;
  88.                     }
  89.                     else
  90.                     {
  91.                        name = "Dragonwrath";
  92.                        materials["motes"] -= 250;
  93.                     }
  94.                     Console.WriteLine($"{name} obtained!");
  95.  
  96.                     foreach (var item in materials.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  97.                     {
  98.                        Console.WriteLine($"{item.Key}: {item.Value}");
  99.                     }
  100.                     foreach (var item in junk.OrderBy(x => x.Key))
  101.                     {
  102.                        Console.WriteLine($"{item.Key}: {item.Value}");
  103.                     }*/
  104.                 foreach (var item in input)
  105.                 {
  106.                     if (char.IsDigit(item[0]) == true)
  107.                     {
  108.                         quantity = int.Parse(item);
  109.                         continue;
  110.                     }
  111.                     if (item == "shards")
  112.                     {
  113.                         if (materials.ContainsKey(item))
  114.                         {
  115.                             materials[item] += quantity;
  116.                             if (materials[item] >= 250)
  117.                             {
  118.                                 materials[item] -= 250;
  119.                                 name = "Shadowmourne";
  120.                                 Console.WriteLine($"{name} obtained!");
  121.                                 foreach (var order in materials.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  122.                                 {
  123.                                     Console.WriteLine($"{order.Key}: {order.Value}");
  124.                                 }
  125.                                 foreach (var order in junk.OrderBy(x => x.Key))
  126.                                 {
  127.                                     Console.WriteLine($"{order.Key}: {order.Value}");
  128.                                 }
  129.                                 return; ;
  130.                             }
  131.                         }
  132.                     }
  133.                     else if (item == "fragments")
  134.                     {
  135.                         if (materials.ContainsKey(item))
  136.                         {
  137.                             materials[item] += quantity;
  138.                             if (materials[item] >= 250)
  139.                             {
  140.                                 materials[item] -= 250;
  141.                                 name = "Valanyr";
  142.                                 Console.WriteLine($"{name} obtained!");
  143.                                 foreach (var order in materials.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  144.                                 {
  145.                                     Console.WriteLine($"{order.Key}: {order.Value}");
  146.                                 }
  147.                                 foreach (var order in junk.OrderBy(x => x.Key))
  148.                                 {
  149.                                     Console.WriteLine($"{order.Key}: {order.Value}");
  150.                                 }
  151.                                 return;
  152.                             }
  153.                         }
  154.                     }
  155.                     else if (item == "motes")
  156.                     {
  157.                         if (materials.ContainsKey(item))
  158.                         {
  159.                             materials[item] += quantity;
  160.                             if (materials[item] >= 250)
  161.                             {
  162.                                 materials[item] -= 250;
  163.                                 name = "Dragonwrath";
  164.                                 Console.WriteLine($"{name} obtained!");
  165.                                 foreach (var order in materials.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  166.                                 {
  167.                                     Console.WriteLine($"{order.Key}: {order.Value}");
  168.                                 }
  169.                                 foreach (var order in junk.OrderBy(x => x.Key))
  170.                                 {
  171.                                     Console.WriteLine($"{order.Key}: {order.Value}");
  172.                                 }
  173.                                 return;
  174.                             }
  175.                         }
  176.  
  177.                     }
  178.                     else
  179.                     {
  180.                         if (junk.ContainsKey(item))
  181.                         {
  182.                             junk[item] += quantity;
  183.                         }
  184.                         else
  185.                         {
  186.                             junk.Add(item, quantity);
  187.                         }
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment