Advertisement
mellowdeep

Light The Torches

Dec 2nd, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class LightTheTorches
  6. {
  7.     public static List<int> torchedRooms = new List<int>();
  8.     public static int nextReturnPosition = 0;
  9.  
  10.     static void Main()
  11.     {
  12.         int roomNumber = int.Parse(Console.ReadLine());
  13.         string roomsInfo = Console.ReadLine();
  14.         Rooms(roomNumber, roomsInfo);
  15.         string command = Console.ReadLine();
  16.         while (command != "END")
  17.         {
  18.             string[] monitoring = command.Split(' ');
  19.             MonitoringMovement(monitoring, nextReturnPosition);
  20.             command = Console.ReadLine();
  21.         }
  22.        Console.WriteLine(torchedRooms.Sum() * Convert.ToInt32('D'));
  23.     }
  24.  
  25.     private static void Rooms(int num, string lightDark)
  26.     {
  27.         int updateRooms = 0;
  28.         for (int i = 0; i < num; i++)
  29.         {
  30.             if (updateRooms > lightDark.Length - 1)
  31.             {
  32.                 updateRooms = 0;
  33.             }
  34.             torchedRooms.Add(lightDark[updateRooms] == 'L' ? 0 : 1);
  35.             updateRooms++;
  36.         }
  37.         nextReturnPosition = num / 2;
  38.     }
  39.  
  40.     private static void MonitoringMovement(string[] arr, int startIndex)
  41.     {
  42.         int roomsToMonitor = Convert.ToInt32(arr[1]);
  43.         if (arr[0] == "LEFT")
  44.         {
  45.             roomsToMonitor = startIndex - roomsToMonitor - 1;
  46.             if (roomsToMonitor < 0)
  47.             {
  48.                 roomsToMonitor = 0;
  49.             }
  50.             if (roomsToMonitor != startIndex)
  51.             {
  52.                 torchedRooms[roomsToMonitor] = torchedRooms[roomsToMonitor] == 0 ? 1 : 0;
  53.             }
  54.         }
  55.  
  56.         if (arr[0] == "RIGHT")
  57.         {
  58.             roomsToMonitor = startIndex + roomsToMonitor + 1;
  59.             if (roomsToMonitor >= torchedRooms.Count)
  60.             {
  61.                 roomsToMonitor = torchedRooms.Count - 1;
  62.             }
  63.             if (roomsToMonitor != startIndex)
  64.             {
  65.                 torchedRooms[roomsToMonitor] = torchedRooms[roomsToMonitor] == 0 ? 1 : 0;
  66.             }
  67.         }
  68.         nextReturnPosition = roomsToMonitor;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement