title: Codingame 'A child's play' process times out
body: <p>I'm trying to solve the coding challenge <a href="https://www.codingame.com/ide/puzzle/a-childs-play" rel="nofollow noreferrer">A child's play</a> on <a href="https://www.codingame.com/home" rel="nofollow noreferrer">Codingame</a> using python.
  With my program I can pass the first two test cases but when the test requires a lot of loops my program goes in timeout. What could I improve?</p>
  
  <p>To fully understand the problem the details of the challenge are needed but I don't want to copy and paste them here because I'm not sure it's allowed.</p>
  
  <p>I try to explain the problem with my words. Given this input:</p>
  
  <pre><code>12 6
  987
  ...#........
  ...........#
  ............
  ............
  ..#O........
  ..........#.
  </code></pre>
  
  <ul>
  <li><code>O</code> is the character starting point.</li>
  <li><code>#</code> are the walls you can not step on</li>
  <li><code>.</code> is where the character can step</li>
  </ul>
  
  <p>In this example w=12 (width of the matrix) and h=6 (height of the matrix).
  n = 987 is the number of steps the character has to take.</p>
  
  <p><strong>Required Output</strong>:</p>
  
  <p>In this case <code>7 1</code> the position of the character after the number of moves given</p>
  
  <p><strong>Rules</strong>:</p>
  
  <ul>
  <li>The character starts always by moving upwards</li>
  <li>When a wall is encountered the character turns clockwise and keeps moving</li>
  <li>The walls are placed so that the caracter can not get stuck and can not exit the matrix.</li>
  </ul>
  
  <p>When I run the program with that test case I get the right result.</p>
  
  <p>With the following test case instead:</p>
  
  <pre><code>14 10
  123456789
  ..#...........
  ....#..#......
  .#O.....#.....
  ..............
  ..............
  .......##...#.
  ............#.
  .#........###.
  .#.#..........
  ..............
  </code></pre>
  
  <p>I get:</p>
  
  <p><em>Failure
  Process has timed out. This may mean that your solution is not optimized enough to handle some cases.</em></p>
  
  <p>This is the code I managed to write:</p>
  
  <pre><code>import math
  import sys
  
  
  def find_initial_position(maze, w, h):
      for i in range(0, h):
          for j in range(0,w):
              if maze[i][j] == "O":
                  return [i, j]
  
      return -1
  
  
  def can_move(maze, direction, x, y):
  
      if direction == "U":
          if maze[ x -1 ][ y ] == "#":
              return False
      elif direction == "R":
          if maze[ x ][ y + 1 ] == "#":
              return False
      elif direction == "D":
          if maze[ x +1 ][ y ] == "#":
              return False
      elif direction == "L":
          if maze[ x ][ y-1 ] == "#":
              return False
  
      return True
  
  
  def turn_clockwise(direction):
      directions = ["U", "R", "D", "L"]
      return directions[ (directions.index(direction) + 1) % 4 ]
  
  
  def move(direction, coordinates):
      if direction == "U":
          coordinates[0] -=1
      elif direction == "R":
          coordinates[1] +=1
      elif direction == "D":
          coordinates[0] +=1
      elif direction == "L":
          coordinates[1] -=1
  
  
  def main():
      w, h = [int(i) for i in input().split()]
      n = int(input())
  
      maze = []
      direction = "U"
      position = [0, 0]
  
      for i in range(h):
          line = input()
          maze.append(line)
  
      position = find_initial_position(maze, w, h)
  
      for i in range(0, n):
          while not can_move(maze, direction, position[0], position[1]):
              direction = turn_clockwise(direction)
  
          move(direction, position)
  
      print( "%(x)d %(y)d" %{"x": position[1], "y": position[0]} )
  
  
  main()
  </code></pre>