17
2
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.
Do we need three outputs if one is never used or will two be ok in that scenario? – Post Rock Garf Hunter – 2020-02-01T02:09:18.963
@PostRockGarfHunter Definitely, I guess that's the same as having three output values one of which never happens. – xnor – 2020-02-01T02:12:49.820
The distinction I would make is that it would allow statically typed languages to output booleans. There is no third value which is not output in that case. – Post Rock Garf Hunter – 2020-02-01T02:17:23.623
5@PostRockGarfHunter Ah, hadn't thought about that. Still totally fine. NASCAR drivers get home with only left turns! – xnor – 2020-02-01T02:20:35.657
1@xnor can the direction be a complex number too (1,-1,i,-i)? – ngn – 2020-02-01T02:24:36.513
@ngn No, I want to stick with four indices for that. – xnor – 2020-02-01T02:26:22.043
@PostRockGarfHunter I think that is still fine in the rules, as you could argue that the other output is given via a particular error (that just won't ever occur). – FryAmTheEggman – 2020-02-01T02:33:08.993