Walk home one step at a time You're at integer coordinates \$(x,y)\$ facing one of North, South, East, or West. Your goal is to walk home to \$(0,0)\$. At each step, you may do one of:


Walk one step in the current facing direction, that is to whichever of \$(x+1,y)\$, \$(x-1,y)\$, \$(x,y-1)\$, or \$(x,y+1)\$ you're facing.
Rotate 90 degrees left, staying in place.
Rotate 90 degrees right, staying in place.


Your goal is to write code that will eventually get you home to \$(0,0)\$ if called repeatedly, each time with your current position and facing. Your code must always give the same output for a given input, which precludes using randomness or past state.

Input:

Two integers \$(x,y)\$ and a facing direction. The 4 possible facing directions are given as numbers 0 through 3, or alternatively 1 through 4, matched as you choose. The position can also be taken as a vector or point or complex number.

You won't be given \$(0,0)\$ where you're already home. Don't worry about overflows caused by huge inputs.

Output:

One of three distinct consistent outputs corresponding to the possible actions of walking straight, turning left, and turning right.
Haskell, 21 bytes



(!!).([(&lt;0),(&gt;0)]&lt;*&gt;)


Try it online!

Outputs a boolean with True being move forward and False being turn right.

Haskell, 28 bytes

Here's a version that is easy to understand / translate into any language.



f(x,y)d=[x&lt;0,y&lt;0,x&gt;0,y&gt;0]!!d


Try it online!

Explanation

The idea here is two-fold


We notice that we only ever need to turn in one direction.  A turn left is just 3 turns right.  So we will limit ourselves to only right turns.
Now we notice that given a direction there is a simple condition that can check if we ought to move forward.  Namely the sign of one of the two variables.


With this information we construct out solution, by making a list of the conditions and indexing the list with the direction.  True means the condition was passed and we can move forward (so we do), False means the condition was failed and we should not move forward, so we rotate.

Now the specification allows us to just output these values as is, since they are distinct and we have no intention of turning left.