Advertisement
DeeAG

Problem2

Mar 3rd, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Problem2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string[] particles = Console.ReadLine()
  11.                 .Split("|", StringSplitOptions.RemoveEmptyEntries)
  12.                 .ToArray();
  13.  
  14.             while (true)
  15.             {
  16.                 string line = Console.ReadLine();
  17.  
  18.                 if (line == "Done")
  19.                 {
  20.                     break;
  21.                 }
  22.  
  23.                 string[] tokens = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
  24.  
  25.                 string command = tokens[0];
  26.                 string instruction = tokens[1];
  27.  
  28.                 if (command == "Move")
  29.                 {
  30.                     int index = int.Parse(tokens[2]);
  31.  
  32.                     if (IsValidIndex(particles, index))
  33.                     {
  34.                         if (instruction == "Left" && IsPossibleMoveLeft(particles, index))
  35.                         {
  36.                             string oldElement = particles[index];
  37.                             particles[index] = particles[index - 1];
  38.                             particles[index - 1] = oldElement;
  39.  
  40.                         }
  41.                         else if (instruction == "Right" && IsPossibleMoveRight(particles, index))
  42.                         {
  43.                             string oldElement = particles[index];
  44.                             particles[index] = particles[index + 1];
  45.                             particles[index + 1] = oldElement;
  46.                         }
  47.                     }
  48.                 }
  49.                 else if (command == "Check")
  50.                 {
  51.                     if (instruction == "Even")
  52.                     {
  53.                         for (int i = 0; i < particles.Length; i++)
  54.                         {
  55.                             if (i % 2 == 0)
  56.                             {
  57.                                 Console.Write($"{particles[i]} ");
  58.                             }
  59.                         }
  60.                         Console.WriteLine();
  61.                     }
  62.                     else if (instruction == "Odd")
  63.                     {
  64.                         for (int i = 0; i < particles.Length; i++)
  65.                         {
  66.                             if (i % 2 == 1)
  67.                             {
  68.                                 Console.Write($"{particles[i]} ");
  69.                             }
  70.                         }
  71.                         Console.WriteLine();
  72.                     }
  73.                 }
  74.             }
  75.             string weapon = string.Concat(particles);
  76.             Console.WriteLine($"You crafted {weapon}!");
  77.  
  78.             //string weapon = String.Empty;
  79.             //foreach (string particle in particles)
  80.             //{
  81.             //    weapon += particle;
  82.             //}
  83.             //Console.WriteLine($"You crafted {weapon}!");
  84.         }
  85.         private static bool IsPossibleMoveRight(string[] parts, int index)
  86.         {
  87.             return index + 1 < parts.Length;
  88.         }
  89.  
  90.         private static bool IsPossibleMoveLeft(string[] parts, int index)
  91.         {
  92.             return index - 1 >= 0;
  93.         }
  94.  
  95.         private static bool IsValidIndex(string[] parts, int index)
  96.         {
  97.             return index >= 0 && index < parts.Length;
  98.         }
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement