Recently I have attended a coding challenge and I was able score only 50%. There were few test cases failed executing my code, I am unable to find reasons for the failure in my code. So I have added the question and my code below. I appreciate your help in find the reason for the failed test cases.
Requirement
Competition for the best cat is coming. The competition consists of several duels. In each duel both cats are standing on an infinite line facing each other. The older cat starts - it has to move towards his opponent by 1 or 2 cat-units. Neither of them can go back, jump over his opponent or share the same spot. The cat owners trained their cats the best they could, so their pets play optimally. The cat that is unable to make a move loses.
Input
First line of the input contains number of tests t (t ≤ 1000). Each test consists of 4 integers: age and position of the first cat and then age and position of the second cat. No two cats have the same age. No calculations in this task will exceed the number
2**31 - 1.
Output
For each test print 1 if the first cat wins and 0 otherwise.
Example
Input:
1 10 1 9 5Output:
0Explanation: Cat
1can move to either field2or3. In the next move cat2regardless of the opponent's decision can block him and win.
My Code
using System;
using System.Linq;
namespace Solution
{
class Solution
{
static void Main(string[] args)
{
var input1 = Console.ReadLine().Trim();
var numOfTest = Convert.ToInt32(input1);
for (int i = 0; i < numOfTest; i++)
{
var input2 = Console.ReadLine().Trim();
var catDetails = input2.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x)).ToArray();
int result = -1;
if (catDetails[0] == catDetails[2])
{
Console.WriteLine(-1);
break;
}
if(catDetails[0] > catDetails[2])
result = CalculateWinningResult(catDetails[0], catDetails[1], catDetails[2], catDetails[3], 1);
else
result = CalculateWinningResult(catDetails[0], catDetails[1], catDetails[2], catDetails[3], 2);
Console.WriteLine(result);
}
}
private static int CalculateWinningResult(int cat1Age, int cat1Position, int cat2Age, int cat2Position, int moveCat)
{
switch (moveCat)
{
case 1:
cat1Position = CalculateCatPosition(cat1Position, cat2Position);
if (cat1Position == 0)
return 0;
else
return CalculateWinningResult(cat1Age, cat1Position, cat2Age, cat2Position, 2);
case 2:
cat2Position = CalculateCatPosition(cat2Position, cat1Position);
if (cat2Position == 0)
return 1;
else
return CalculateWinningResult(cat1Age, cat1Position, cat2Age, cat2Position, 1);
}
return 0;
}
private static int CalculateCatPosition(int currentCatPosition, int opponentCatPosition)
{
int tempPosition;
if (currentCatPosition > opponentCatPosition)
{
tempPosition = (currentCatPosition - 1);
if (tempPosition <= opponentCatPosition)
return 0;
else
{
tempPosition = (currentCatPosition - 2);
if (tempPosition >= opponentCatPosition)
return 0;
else
currentCatPosition = tempPosition;
}
}
else
{
tempPosition = (currentCatPosition + 1);
if (tempPosition <= opponentCatPosition)
return 0;
else
{
tempPosition = (currentCatPosition + 2);
if (tempPosition >= opponentCatPosition)
return 0;
else
currentCatPosition = tempPosition;
}
}
return currentCatPosition;
}
}
}
