Advertisement
enevlogiev

LightTheTorches

Aug 22nd, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. namespace Problem04_LightTheTorches
  2. {
  3.     using System;
  4.  
  5.     public class LightTheTorches
  6.     {
  7.         static void Main()
  8.         {
  9.             int numberOfRooms = int.Parse(Console.ReadLine());
  10.             string initialState = Console.ReadLine();
  11.  
  12.             char[] basement = new char[numberOfRooms];
  13.  
  14.  
  15.             for (int index = 0; index < basement.Length; index++)
  16.             {
  17.                 // this formula will keep the index in range. Division with % will always
  18.                 // return an index that is part of the string
  19.                 int stringIndex = index % initialState.Length;
  20.                 basement[index] = initialState[stringIndex];
  21.             }
  22.  
  23.             //find the middle of the basement - the initial position of Haralampi
  24.             int currentPosition = numberOfRooms / 2;
  25.  
  26.             string command = Console.ReadLine();
  27.             while (command != "END")
  28.             {
  29.                 string[] commandTokens = command.Split(' ');
  30.                 string direction = commandTokens[0];
  31.                 int steps = int.Parse(commandTokens[1]);
  32.  
  33.                 // this is required by the task - Haralampi always moves one room more
  34.                 steps += 1;
  35.  
  36.                 // we need to check if Haralampi has moved
  37.                 int oldPosition = currentPosition;
  38.  
  39.  
  40.                 // if he needs to go left, we need to substract from his current position
  41.                 if (direction == "LEFT")
  42.                 {
  43.                     steps = steps * -1;
  44.                 }
  45.  
  46.                 currentPosition += steps;      
  47.  
  48.                 // keep Haralampi in the basement!  Hint - we can also use Math.Max and Math.Min : )
  49.                 if (currentPosition >= numberOfRooms)
  50.                 {
  51.                     currentPosition = numberOfRooms - 1;
  52.                 }
  53.                 if (currentPosition < 0)
  54.                 {
  55.                     currentPosition = 0;
  56.                 }
  57.  
  58.                 // if Haralampi hasn't moved, skip changing the torches
  59.                 if (oldPosition == currentPosition)
  60.                 {
  61.                     command = Console.ReadLine();
  62.                     continue;
  63.                 }
  64.  
  65.                 if (basement[currentPosition] == 'L')
  66.                 {
  67.                     basement[currentPosition] = 'D';
  68.                 }
  69.                 else
  70.                 {
  71.                     basement[currentPosition] = 'L';                  
  72.                 }
  73.  
  74.  
  75.                 command = Console.ReadLine();
  76.             }
  77.  
  78.             int darkRooms = 0;
  79.  
  80.             foreach (char room in basement)
  81.             {
  82.                 if (room == 'D')
  83.                 {
  84.                     darkRooms++;
  85.                 }
  86.             }
  87.  
  88.             int ASCIICodeOfLetterD = 'D';
  89.             Console.WriteLine(darkRooms * ASCIICodeOfLetterD);
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement