Advertisement
fbinnzhivko

04.03 Light the Torches

May 4th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. class Torches
  3. {
  4.     static int position1;
  5.     static void Main()
  6.     {
  7.         int n = int.Parse(Console.ReadLine());
  8.         string input = Console.ReadLine();
  9.         string[] command = Console.ReadLine().Split();
  10.         char[] rooms = new char[n];
  11.         FillRooms(input, rooms);
  12.         position1 = n / 2;
  13.  
  14.         while (command[0] != "END")
  15.         {
  16.             if (command[0] == "RIGHT")
  17.             {
  18.                 if (position1 + int.Parse(command[1].ToString()) + 1 > rooms.Length - 1)
  19.                 {
  20.                     int position2 = rooms.Length - 1;
  21.                     if (position1 != position2)
  22.                     {
  23.                         position1 = rooms.Length - 1;
  24.                         SwitchLights(rooms, position1);
  25.                     }
  26.                 }
  27.                 else
  28.                 {
  29.                     position1 = position1 + int.Parse(command[1].ToString()) + 1;
  30.                     SwitchLights(rooms, position1);
  31.                 }
  32.             }
  33.             else if (command[0] == "LEFT")
  34.             {
  35.                 if (position1 - int.Parse(command[1].ToString()) - 1 < 0)
  36.                 {
  37.                     int position2 = 0;
  38.                     if (position1 != position2)
  39.                     {
  40.                         position1 = 0;
  41.                         SwitchLights(rooms, position1);
  42.                     }
  43.                 }
  44.                 else
  45.                 {
  46.                     position1 = position1 - int.Parse(command[1].ToString()) - 1;
  47.                     SwitchLights(rooms, position1);
  48.                 }
  49.             }
  50.             command = Console.ReadLine().Split();
  51.         }
  52.         int darkCounter = 0;
  53.         for (int i = 0; i < rooms.Length; i++)
  54.         {
  55.             if (rooms[i] == 'D')
  56.             {
  57.                 darkCounter++;
  58.             }
  59.         }
  60.         Console.WriteLine(darkCounter * (int)'D');
  61.     }
  62.  
  63.     private static void FillRooms(string input, char[] rooms)
  64.     {
  65.         int counter = 0;
  66.         for (int i = 0; i < rooms.Length; i++)
  67.         {
  68.             rooms[i] = input[counter % input.Length];
  69.             counter++;
  70.         }
  71.     }
  72.  
  73.     private static void SwitchLights(char[] rooms, int position1)
  74.     {
  75.         if (rooms[position1] == 'L')
  76.         {
  77.             rooms[position1] = 'D';
  78.         }
  79.         else
  80.         {
  81.             rooms[position1] = 'L';
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement