<p>Your car starts at position <code>0</code> and speed <code>+1</code> on an infinite number line. Your car can go into negative positions. Your car drives automatically according to a sequence of instructions <code>&#39;A&#39;</code> (accelerate) and <code>&#39;R&#39;</code> (reverse):</p>

<ul>
	<li>When you get an instruction <code>&#39;A&#39;</code>, your car does the following:

	<ul>
		<li><code>position += speed</code></li>
		<li><code>speed *= 2</code></li>
	</ul>
	</li>
	<li>When you get an instruction <code>&#39;R&#39;</code>, your car does the following:
	<ul>
		<li>If your speed is positive then <code>speed = -1</code></li>
		<li>otherwise <code>speed = 1</code></li>
	</ul>
	Your position stays the same.</li>
</ul>

<p>For example, after commands <code>&quot;AAR&quot;</code>, your car goes to positions <code>0 --&gt; 1 --&gt; 3 --&gt; 3</code>, and your speed goes to <code>1 --&gt; 2 --&gt; 4 --&gt; -1</code>.</p>

<p>Given a target position <code>target</code>, return <em>the length of the shortest sequence of instructions to get there</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> target = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
The shortest instruction sequence is &quot;AA&quot;.
Your position goes from 0 --&gt; 1 --&gt; 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> target = 6
<strong>Output:</strong> 5
<strong>Explanation:</strong> 
The shortest instruction sequence is &quot;AAARA&quot;.
Your position goes from 0 --&gt; 1 --&gt; 3 --&gt; 7 --&gt; 7 --&gt; 6.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= target &lt;= 10<sup>4</sup></code></li>
</ul>

from collections import deque 

class Solution:
    def racecar(self, target: int) -> int:
        
        #pos = 0, speed = 1, command = 0 
        queue = deque([(0, 1, 0)])
        
        #track the positions and speeds 
        visited = set([(0,1)])
        
        while queue: 
            pos, speed, command = queue.popleft() 
            
            #check if we have already reached the target num
            if pos == target: 
                return command 
            
            #accelerate 
            
            new_pos = pos + speed 
            new_speed = speed * 2
            
            # then we have to check if this has been visited before 
            if (new_pos, new_speed) not in visited: 
                visited.add((new_pos, new_speed))
                
                queue.append((new_pos, new_speed, command + 1))
            #reverse 
            new_speed = -1 if speed >0 else 1
            if (pos, new_speed) not in visited: 
                visited.add((pos, new_speed))
                queue.append((pos, new_speed, command + 1 ))
                
            
                
        

#         queue = deque([(0,0,1)])
#         visited = set()
#         while queue:
#             moves, position,speed = queue.popleft()
#             if position == target:
#                 return moves
#             if (position,speed) in visited:
#                 continue
#             else:
#                 visited.add((position,speed))
#                 queue.append((moves+1,position+speed,speed*2))
#                 if (position+speed > target and speed > 0) or (position+speed < target and speed < 0):
#                     speed = -1 if speed > 0 else 1
#                     queue.append((moves+1,position,speed))
#         return 0
316065