Skip to content

Instantly share code, notes, and snippets.

@nasko90
Created December 3, 2016 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasko90/31049bc6f40fa3c7aa01bd2ca01e6288 to your computer and use it in GitHub Desktop.
Save nasko90/31049bc6f40fa3c7aa01bd2ca01e6288 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Methods
{
class Program
{
static void Main()
{
string numbers = Console.ReadLine();
string instructions = "";
int times = 0;
int steps = 0;
bool parse;
int[] numArray = numbers.Split(' ').Select(c => Convert.ToInt32(c.ToString())).ToArray();
int startingPosition = 0;
double value = 0;
int position = 0;
double result = 0;
double rounds = 0;
int positionOut = 0;
int changeSteps = 0;
while (true)
{
instructions = Console.ReadLine();
if (instructions == "stop")
{
break;
}
rounds++;
string[] instructionsToArray = instructions.Split(' ');
parse = Int32.TryParse(instructionsToArray[0], out times);
parse = Int32.TryParse(instructionsToArray[2], out steps);
int count = 0;
bool isRightDirection = true;
if (instructionsToArray[1] == "left")
{
isRightDirection = false;
}
if (isRightDirection)
{
for (position = startingPosition; count < times; count++)
{
positionOut += steps;
if (positionOut >= numArray.Length)
{
if (steps>numArray.Length)
{
changeSteps = steps % numArray.Length;
}
else
{
changeSteps = steps;
}
for (int i = 0; i < changeSteps; i++)
{
position++;
if (position==numArray.Length)
{
position = 0;
}
}
positionOut = position;
}
else
{
position = positionOut;
}
value += numArray[position];
}
}
else
{
for (position = startingPosition; count < times; count++)
{
positionOut -= steps;
if (positionOut < 0)
{
if (steps > numArray.Length)
{
changeSteps = steps % numArray.Length;
}
else
{
changeSteps = steps;
}
for (int i = 0; i < changeSteps; i++)
{
position--;
if (position == -1)
{
position = numArray.Length-1;
}
}
positionOut = position;
}
else
{
position = positionOut;
}
value += numArray[position];
}
}
startingPosition = position;
}
result = value / rounds;
Console.WriteLine(result.ToString("0.0"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment