JulianJulianov

16.TextProcessingMoreExercise-Treasure Finder

Apr 25th, 2020
519
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 03. Treasure Finder
  2. Write a program that decrypts a message by a given key and gathers information about hidden treasure type and its coordinates. On the first line you will receive a key (sequence of numbers). On the next few lines until you receive "find" you will get lines of strings. You have to loop through every string and decrease the ascii code of each character with a corresponding number of the key sequence. The way you choose a key number from the sequence is just looping through it. If the length of the key sequence is less than the string sequence, you start looping from the beginning of the key. For more clarification see the example below. After decrypting the message you will get a type of treasure and its coordinates. The type will be between the symbol '&' and the coordinates will be between the symbols '<' and '>'. For each line print the type and the coordinates in format "Found {type} at {coordinates}".
  3. Example
  4. Input                                  Output                    Comment
  5. 1 2 1 3                                Found gold at 10N70W      We start looping through the first string and the key. When we reach
  6. ikegfp'jpne)bv=41P83X@                 Found Silver at 32S43W    the end of the key we start looping from the beginning of the key,
  7. ujfufKt)Tkmyft'duEprsfjqbvfv=53V55XA                             but we continue looping through the string. (until the string is over)
  8. find                                                             The first message is: "hidden&gold&at<10N70W>" so we print we found
  9.                                                                  gold at the given coordinates. We do the same for the second string
  10.                                                                  "thereIs&Silver&atCoordinates<32S43W>"(starting from the beginning of
  11.                                                                  the key and the beginning of the string).
  12. using System;
  13.                    
  14. public class Program
  15. {
  16.      public static void Main(string[] args)
  17.      {
  18.            var key = Console.ReadLine().Split();
  19.  
  20.             var decryptsMessage = "";
  21.             while ((decryptsMessage = Console.ReadLine()) != "find")
  22.             {
  23.                 var message = "";
  24.                 var index = 0;
  25.                 foreach (var item in decryptsMessage)
  26.                 {
  27.                     var decryptsSymbol = (char)(item - int.Parse(key[index]));
  28.                     message += decryptsSymbol;
  29.                     index++;
  30.                     if (index == key.Length)
  31.                     {
  32.                         index = 0;
  33.                     }
  34.                 }
  35.                 var startIndexTreasure = message.IndexOf('&');         //IndexOfAny(...) може да се замени с LastIndexOf('&')
  36.                 var findNextIndexTreasure = message.IndexOfAny(message[startIndexTreasure].ToString().ToCharArray(), startIndexTreasure + 1);
  37.                 var treasureType = message.Substring(startIndexTreasure + 1, findNextIndexTreasure - startIndexTreasure - 1);
  38.  
  39.                 var startIndex = message.IndexOf('<');
  40.                 var endIndex = message.IndexOf('>');
  41.                 var coordinates = message.Substring(startIndex + 1, endIndex - startIndex - 1);
  42.  
  43.                 Console.WriteLine($"Found {treasureType} at {coordinates}");
  44.             }
  45.            
  46.      }
  47. }
RAW Paste Data