Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Problem04_LightTheTorches
- {
- using System;
- public class LightTheTorches
- {
- static void Main()
- {
- int numberOfRooms = int.Parse(Console.ReadLine());
- string initialState = Console.ReadLine();
- char[] basement = new char[numberOfRooms];
- for (int index = 0; index < basement.Length; index++)
- {
- // this formula will keep the index in range. Division with % will always
- // return an index that is part of the string
- int stringIndex = index % initialState.Length;
- basement[index] = initialState[stringIndex];
- }
- //find the middle of the basement - the initial position of Haralampi
- int currentPosition = numberOfRooms / 2;
- string command = Console.ReadLine();
- while (command != "END")
- {
- string[] commandTokens = command.Split(' ');
- string direction = commandTokens[0];
- int steps = int.Parse(commandTokens[1]);
- // this is required by the task - Haralampi always moves one room more
- steps += 1;
- // we need to check if Haralampi has moved
- int oldPosition = currentPosition;
- // if he needs to go left, we need to substract from his current position
- if (direction == "LEFT")
- {
- steps = steps * -1;
- }
- currentPosition += steps;
- // keep Haralampi in the basement! Hint - we can also use Math.Max and Math.Min : )
- if (currentPosition >= numberOfRooms)
- {
- currentPosition = numberOfRooms - 1;
- }
- if (currentPosition < 0)
- {
- currentPosition = 0;
- }
- // if Haralampi hasn't moved, skip changing the torches
- if (oldPosition == currentPosition)
- {
- command = Console.ReadLine();
- continue;
- }
- if (basement[currentPosition] == 'L')
- {
- basement[currentPosition] = 'D';
- }
- else
- {
- basement[currentPosition] = 'L';
- }
- command = Console.ReadLine();
- }
- int darkRooms = 0;
- foreach (char room in basement)
- {
- if (room == 'D')
- {
- darkRooms++;
- }
- }
- int ASCIICodeOfLetterD = 'D';
- Console.WriteLine(darkRooms * ASCIICodeOfLetterD);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement