title: Programming challenge: Competition for the best cat is coming
body: <p>Recently I have attended a <em>coding challenge</em> and I was able score only 50%. There were few test cases failed executing my code, I am unable to <em>find reasons</em> for the failure in my code. So I have added the question and my code below. I appreciate your help in find the <em>reason for the failed test cases</em>. </p>
  
  <p><strong>Requirement</strong></p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p><strong>Input</strong></p>
  
  <blockquote>
    <p>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 <code>2**31 - 1</code>.</p>
  </blockquote>
  
  <p><strong>Output</strong></p>
  
  <blockquote>
    <p>For each test print 1 if the first cat wins and 0 otherwise.</p>
  </blockquote>
  
  <p><strong>Example</strong></p>
  
  <blockquote>
    <p><strong>Input:</strong></p>
  
  <pre><code>1
  10 1 9 5
  </code></pre>
    
    <p><strong>Output:</strong></p>
  
  <pre><code>0
  </code></pre>
    
    <p><strong>Explanation:</strong> Cat <code>1</code> can move to either field <code>2</code> or <code>3</code>. In the next move
    cat <code>2</code> regardless of the opponent's decision can <em>block</em> him and <em>win</em>.</p>
  </blockquote>
  
  <p><strong>My Code</strong></p>
  
  <pre><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 &lt; numOfTest; i++)
              {
                  var input2 = Console.ReadLine().Trim();
                  var catDetails = input2.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x =&gt; Convert.ToInt32(x)).ToArray();
                  int result = -1;
  
                  if (catDetails[0] == catDetails[2])
                  {
                      Console.WriteLine(-1);
                      break;
                  }
  
                  if(catDetails[0] &gt; 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 &gt; opponentCatPosition)
              {
                  tempPosition = (currentCatPosition - 1);
  
                  if (tempPosition &lt;= opponentCatPosition)
                      return 0;
                  else
                  {
                      tempPosition = (currentCatPosition - 2);
                      if (tempPosition &gt;= opponentCatPosition)
                          return 0;
                      else
                          currentCatPosition = tempPosition;
                  }
              }
              else
              {
                  tempPosition = (currentCatPosition + 1);
  
                  if (tempPosition &lt;= opponentCatPosition)
                      return 0;
                  else
                  {
                      tempPosition = (currentCatPosition + 2);
                      if (tempPosition &gt;= opponentCatPosition)
                          return 0;
                      else
                          currentCatPosition = tempPosition;
                  }
              }
  
              return currentCatPosition;
          }
      }
  }
  </code></pre>
  
  <p><strong>Result</strong><a href="https://i.stack.imgur.com/IJH5t.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IJH5t.png" alt="test case result"></a></p>