settingsAccountsettings
Menusettings

Q: Ani Is Drunk - C# Task

+6 votes

Ani is drunk as boot. She is at the disco and she needs to throw up like immediately. However, she is a civilized young lady so she wants to do it in the toilets. Unfortunately, at the disco all the toilet cabins are always full. So, she has to spend quite some time to find a free cabin. She is also hearing a voice that tells her how many steps she needs to go to the next cabin to check. There is a problem, though. The voice is telling her to go to the right only, and the number of steps is more than the number of cabins. The idea is that if she is at the last cabin and has to go a few more steps, she starts over at the first cabin.

She is so drunk that she can’t math. Help her with the calculations.

The first cabin is always at number zero, the last is numberOfCabins - 1

 For example, we have 5 cabins; she is currently at cabin #2, and needs to go 4 steps to the right. She passes by cabin #3 and #4, and reaches the end. She needs to start over at #0 and stops at cabin #1. She didn’t have to go so many steps; she could just move from cabin #2 to cabin #1. Your task is to calculate the next cabin and tell her which direction to go and how many steps to take.

Input

At the first line, you will receive an integer N – the number of cabins.
At the next K lines, you will receive an integer S – the number of steps she need to take.
At the last line, you will receive the command “Found a free one”, which denotes end of the input.

Output

For each K line, print the actual number of steps that she needs to take, in one of the following formats:

  • “Go {0} steps to the right, Ani.”
  • “Go {0} steps to the left, Ani.”
  • “Stay there, Ani.”

At the last line, you must print to total number of steps done by Ani, in format:

  • “Moved a total of {0} steps.”

Constraints

  • The number N is an integer in the range [0… 4,000,000,000] inclusive.
  • The number S is an integer in the range [0… 4,000,000,000] inclusive.
  • There will be no more than 20,000 lines of K
  • Allowed work time for your program: 0.1 seconds.
  • Allowed memory: 16 MB.

Examples

Drunk Ani C# Task

asked in C# category by user hues
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Here's my friend - the solution:

using System;

namespace _02.DrunkAni
{
    class DrunkAni
    {
        static void Main(string[] args)
        {
            long numberOfCabins = long.Parse(Console.ReadLine());
            long currentPosition = 0;
            long totalLenghtCovered = 0;
            long stepsCount = 0;

            string nextCommand = null;
            while (nextCommand != "Found a free one!")
            {
                nextCommand = Console.ReadLine();
                if (nextCommand == "Found a free one!")
                {
                    break;
                }
                stepsCount = int.Parse(nextCommand);

                long oldPosition = currentPosition;
                currentPosition = (currentPosition + stepsCount) % numberOfCabins;
                long difference = currentPosition - oldPosition;

                if (difference > 0)
                {
                    Console.WriteLine("Go {0} steps to the right, Ani.", Math.Abs(difference));
                }
                else if (difference < 0)
                {
                    Console.WriteLine("Go {0} steps to the left, Ani.", Math.Abs(difference));

                }
                else
                {
                    Console.WriteLine("Stay there, Ani.");

                }

                totalLenghtCovered += Math.Abs(difference);
            }
            Console.WriteLine("Moved a total of {0} steps.", Math.Abs(totalLenghtCovered));
        }
    }
}

 

answered by user john7
selected by user golearnweb
...