Advertisement
ivo_petkov01

Treasure Hunt

Feb 16th, 2023
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _02._Treasure_Hunt
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> treasureChest = Console.ReadLine()
  12.                 .Split("|", StringSplitOptions.RemoveEmptyEntries)
  13.                 .ToList();
  14.  
  15.             string commandLine = Console.ReadLine();
  16.             bool isSuccessful = true;
  17.  
  18.             while (commandLine != "Yohoho!")
  19.             {
  20.                 string[] tokens = commandLine.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  21.                 string currentCommand = tokens[0];
  22.  
  23.                 if (currentCommand == "Loot")
  24.                 {
  25.                     for (int i = 1; i < tokens.Length; i++)
  26.                     {
  27.                         string currentItem = tokens[i];
  28.  
  29.                         if (!treasureChest.Contains(currentItem))
  30.                         {
  31.                             treasureChest.Insert(0, currentItem);
  32.                         }
  33.                     }
  34.                 }
  35.                 else if (currentCommand == "Drop")
  36.                 {
  37.                     int indexToDrop = int.Parse(tokens[1]);
  38.  
  39.                     if (indexToDrop >= 0 && indexToDrop < treasureChest.Count)
  40.                     {
  41.                         string itemToDrop = treasureChest.ElementAt(indexToDrop);
  42.                         treasureChest.RemoveAt(indexToDrop);
  43.                         treasureChest.Add(itemToDrop);
  44.                     }
  45.                 }
  46.                 else if (currentCommand == "Steal")
  47.                 {
  48.                     int count = int.Parse(tokens[1]);
  49.  
  50.                     if (count < treasureChest.Count)
  51.                     {
  52.                         string stolenItems = string.Empty;
  53.  
  54.                         for (int i = treasureChest.Count - count; i < treasureChest.Count; i++)
  55.                         {
  56.                             string stolenItem = treasureChest[i];
  57.  
  58.                             if (i == treasureChest.Count - 1)
  59.                             {
  60.                                 stolenItems += stolenItem;
  61.                             }
  62.                             else
  63.                             {
  64.                                 stolenItems += stolenItem + ", ";
  65.                             }
  66.                         }
  67.  
  68.                         treasureChest.RemoveRange(treasureChest.Count - count, count);
  69.                         Console.WriteLine(stolenItems);
  70.                     }
  71.                     else
  72.                     {
  73.                         Console.WriteLine(string.Join(", ", treasureChest));
  74.                         Console.WriteLine("Failed treasure hunt.");
  75.                         isSuccessful = false;
  76.                         break;
  77.                     }
  78.                 }
  79.  
  80.                 commandLine = Console.ReadLine();
  81.             }
  82.  
  83.             if (isSuccessful)
  84.             {
  85.                 int sumItems = 0;
  86.  
  87.                 foreach (string item in treasureChest)
  88.                 {
  89.                     sumItems += item.Length;
  90.                 }
  91.  
  92.                 double averageGain = sumItems / (double)treasureChest.Count;
  93.  
  94.                 Console.WriteLine($"Average treasure gain: {averageGain:F2} pirate credits.");
  95.             }
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement