TEST

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
2
Indexable
Never
class Program
{
    static int GetDistanceFromEnemy(int[] input)
    {
        var enemyPos = 0;
        var mePos = 0;
        for (var i = 0; i < input.Length; i++)
        {
            if (input[i] == 1)
            {
                mePos = i;
                var breakTrue = false;
                for (var j = 0; j < input.Length; j++)
                {
                    if (input[j] == 1)
                        breakTrue = true;

                    if (input[j] == 2)
                    {
                        enemyPos = j;
                        if (breakTrue)
                            break;
                    }
                }
                break;
            }
        }
        return Math.Abs(enemyPos - mePos);
    }

    static void Main(string[] args)
    {
        var a = GetDistanceFromEnemy(new int[] { 0, 0, 1, 0, 0, 2, 0, 2 });
        var b = GetDistanceFromEnemy(new int[] { 1, 0, 0, 0, 2, 2, 2 });
        var c = GetDistanceFromEnemy(new int[] { 2, 0, 0, 0, 2, 2, 1, 0 });
        Console.WriteLine("Distance from enemy --> a:" + a);
        Console.WriteLine("Distance from enemy --> b:" + b);
        Console.WriteLine("Distance from enemy --> c:" + c);
    }
}