title: Codingame : A child's play
body: <p>I have a problem with a puzzle named &quot;<a href="https://www.codingame.com/training/easy/a-childs-play" rel="nofollow noreferrer">A child's play</a>&quot; of <a href="https://www.codingame.com/home" rel="nofollow noreferrer">Codingame</a>. <strong>I code in typescript !</strong></p>
  <p>The statement is :</p>
  <blockquote>
  <p>For several years now, in elementary schools, we have seen the emergence of a new educational model, playful programming. The students must program a small robot using assembly blocks. This allows them to get familiar with programming from an early age while exercising their logic and perception of space.</p>
  </blockquote>
  <blockquote>
  <p>You are a student at one such school. The purpose of the exercise is simple: your teacher has crafted a circuit for your robot, told you how many moves n the robot may make, and you must find out the final position of the robot at end of execution.</p>
  </blockquote>
  <blockquote>
  <p>To do this you need to know some principles of robot operation.<br/>
  – When the robot encounters an obstacle (represented by #) it turns right (on same operation) until there's no obstacle ahead anymore. Otherwise on an empty area (represented by .) it moves straight ahead.<br/>
  – The robot initially moves upwards.<br/>
  – The robot stops after n moves.<br/>
  – The top left corner represents the coordinates (0,0)<br/>
  – The robot's environment is represented as follows, where O is the robot's initial position:</p>
  </blockquote>
  <blockquote>
  <p>...#........ <br/>
  ...........# <br/>
  ............ <br/>
  ............ <br/>
  ..#O........ <br/>
  ..........#.</p>
  </blockquote>
  <p>I'm block at the test 4, because my solution is not optimized.</p>
  <p>It gives me in the console: <em><strong>The process timed out. This may mean that your solution is not optimized enough to handle some cases</strong></em>.</p>
  <p>I tried to change the loop for a &quot;while&quot; loop and change and change the &quot;switch&quot; condition, for an &quot;if-else&quot; condition.</p>
  <p>Can you help me to find a better solution or a other way to do the tests?</p>
  <pre><code>var inputs: string[] = readline().split(' ');
  const w: number = parseInt(inputs[0]);
  const h: number = parseInt(inputs[1]);
  const n: number = parseInt(readline());
  let zone: string[][] = [];
  let robot: robot = { x: 0, y: 0 };
  
  interface robot {
      x: number,
      y: number
  }
  for (let i = 0; i &lt; h; i++) {
      const line: string = readline();
      zone = [...zone, line.split('')]
  }
  
  zone.forEach((line, y) =&gt; {
      line.forEach((place, x) =&gt; {
          if (place === &quot;O&quot;) {
              robot.x = x;
              robot.y = y;
          }
      })
  })
  
  function getLoc(robot: robot, zone: string[][], tours: number) {
      let direct: string = &quot;T&quot;;
      var i = 0;
      while (i &lt; tours) {
          if (direct === &quot;T&quot;) {
              if (zone[robot.y - 1][robot.x] === '#') {
                  robot.x++;
                  direct = &quot;R&quot;;
              } else {
                  robot.y--;
              }
          } else if (direct === &quot;R&quot;) {
              if (zone[robot.y][robot.x + 1] === '#') {
                  robot.y++;
                  direct = &quot;B&quot;;
              } else {
                  robot.x++;
              }
          } else if (direct === &quot;B&quot;) {
              if (zone[robot.y + 1][robot.x] === '#') {
                  robot.x--;
                  direct = &quot;L&quot;;
              } else {
                  robot.y++;
              }
          } else if (direct === &quot;L&quot;) {
              if (zone[robot.y][robot.x - 1] === '#') {
                  robot.y--;
                  direct = &quot;T&quot;;
              } else {
                  robot.x--;
              }
          }
          i++;
      }
      return robot
  }
  
  console.time(&quot;getLoc&quot;)
  let res: robot = getLoc(robot, zone, n);
  console.timeEnd(&quot;getLoc&quot;)
  console.log(`${res.x} ${res.y}`) 
  </code></pre>