Advertisement
Guest User

Untitled

a guest
Mar 21st, 2022
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace TreasureHunt
  5. {
  6.     internal class Program
  7.     {
  8.         private static void Main(string[] args)
  9.         {
  10.             var chestItems = Console.ReadLine().Split('|', StringSplitOptions.RemoveEmptyEntries).ToList();
  11.  
  12.             while (true)
  13.             {
  14.                 var inputLine = Console.ReadLine();
  15.  
  16.                 if (!chestItems.Any())
  17.                 {
  18.                     Console.WriteLine("Failed treasure hunt.");
  19.                     return;
  20.                 }
  21.  
  22.                 if (inputLine == "Yohoho!")
  23.                     break;
  24.  
  25.                 var inputList = inputLine.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList();
  26.                 var command = inputList[0];
  27.  
  28.                 switch (command)
  29.                 {
  30.                     case "Loot":
  31.                         inputList.RemoveAt(0);
  32.                         inputList.ForEach(x =>
  33.                         {
  34.                             if (!chestItems.Contains(x))
  35.                                 chestItems.Insert(0, x);
  36.                         });
  37.                         break;
  38.                     case "Drop":
  39.                         var itemIndex = int.Parse(inputList[1]);
  40.                         var item = chestItems.ElementAtOrDefault(itemIndex);
  41.                         if (item != null)
  42.                         {
  43.                             chestItems.RemoveAt(itemIndex);
  44.                             chestItems.Add(item);
  45.                         }
  46.  
  47.                         break;
  48.                     case "Steal":
  49.                         var stealCount = int.Parse(inputList[1]);
  50.                         var chestCount = chestItems.Count();
  51.  
  52.                         if (chestCount < stealCount)
  53.                             stealCount = chestCount;
  54.  
  55.                         if (chestItems.Any())
  56.                         {
  57.                             var stealStartIndex = chestCount - stealCount;
  58.                             Console.WriteLine(string.Join(", ", chestItems.GetRange(stealStartIndex, stealCount)));
  59.                             chestItems.RemoveRange(stealStartIndex, stealCount);
  60.                         }
  61.  
  62.                         break;
  63.                 }
  64.             }
  65.  
  66.             var chestItemsSum = 0.00;
  67.             chestItems.ForEach(x => { chestItemsSum += x.Length; });
  68.  
  69.             var averageGain = chestItemsSum / chestItems.Count();
  70.  
  71.             Console.WriteLine($"Average treasure gain: {averageGain:f2} pirate credits.");
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement