Tag Info

New answers tagged grid

1

05AB1E, 4 bytes d2βQ Port of @NickKennedy's Jelly answer, so make sure to upvote him as well! First input is the coordinate, second input is the current direction digit (in the range [0,3]). Outputs 0 to turn, and 1 to move forward. Try it online or verify a random coordinate will eventually result in [0,0] or verify all coordinates in the range [[-1,1],[...

0

C (gcc), 47 bytes z;m(x,d)int*x;{x=abs(z=x[d/2])>abs(z+d%2*2-1);} Returns 0 to move forward and 1 to turn clockwise. It never moves counterclockwise but let's just call that 2. North is 3, south is 2, east is 1, and west is 0. -2 bytes thanks to ceilingcat! Try it online!

1

Retina 0.8.2, 38 35 bytes . (-?).*( -?).* $*-$2$1$1 ^(-*) \1\$ Try it online! Link includes demonstration of path taken from 5 3 to the origin. Takes input as direction first, then coordinates. Edit: Saved 3 bytes by porting @isaacg’s Pyth answer. Direction encodes as y--, x--, x++, y++. Outputs 1 to move, 0 to turn. Explanation: . (-?).*( -?).* Capture ...

4

Jelly, 4 bytes >-Ḅ⁼ Try it online! A dyadic link taking the co-ordinates as the left argument and the direction faced as the right. Returns 0 for turn and 1 for move forwards. The TIO footer picks a random starting point and demonstrates that the link will ultimately reach [0,0] Based on @isaacg’s Pyth answer so be sure to upvote that one too! ...

0

Charcoal, 13 9 bytes ⁼↨Ｅ²›⁰Ｎ²Ｎ Try it online! Link is to verbose version of code. Edit: Saved 4 bytes by porting @isaacg’s Pyth answer. Uses the same "move or turn" approach; - means move, no output means turn. The directions encode as y--, x--, x++, y++. Explanation: ² Literal 2 Ｅ Map over implicit range ⁰ Literal 0 › ...

3

JavaScript (Node.js), 26 25 24 bytes (x,y,d)=>--d%2*x<--d%2*y Try it online! d is direction one of [0, 1, 2, 3] for [west, north, east, south] The expression (d - 1) % 2 gives deltaX one of [-1, 0, 1, 0]and(d - 2) % 2 gives deltaY one of [0, -1, 0, 1] Either x * deltaX or y * deltaY will be non zero and will be negative if we are heading towards ...

2

JavaScript (ES6),  28  27 bytes Takes input as (x,y,d) with $d=0$ for North, $1$ for South, $2$ for West, $3$ for East. Output: true : turn right false : walk straight (x,y,d)=>(x&&2|x<0||y<0)!=d Try it online! How? This is parsed as: +--------------------> 0 if x = 0, 3 if x < 0, 2 if x > 0 ...

7

Pyth, 7 bytes qi<L0E2 Try it online! Input is in the form: 3 [3, 4] The solution is simple: <L0E maps each coordinate to 0 if the coordinate is positive, and 1 if the coordinate is nonpositive. i ... 2 converts the list to a number by interpreting it as binary. q checks whether the resulting number is equal to the other input. True means forward, ...

4

Python 2, 30 bytes lambda i,d:abs(i+1j**d)<abs(i) Try it online! Takes as input a Gaussian integer for position, and an integer in {0,1,2,3} for the direction; outputs True for forward, False for rotate counter-clockwise, and whatever you like for rotate clockwise, because that never happens :).

3

APL (Dyalog Classic), 10 9 bytes ⊣⊃0(>,<)⊢ Try it online! all credit to Post Rock Garf Hunter's answer the tio link shows numbers of steps for the square area around (0,0) for the 4 initial directions ⊣ left arg: direction ⊢ right arg: coord pair , concatenation 0(>,<)⊢ is equivalent to (0>⊢),(0<⊢), i.e. the 4-vector (0>x),(0>...

3

J, 12 11 bytes [>&|[+0j1^] Try it online! idea Our idea is simple: Walk straight, and see if that gets us closer in Euclidean distance. If yes, return "walk straight". If not, return "turn left". input / output Take position as a complex number (left arg) and direction as a 0-indexed int (right arg). Similar to @PostRockGarfHunter's answer, we ...

12

Haskell, 21 bytes (!!).([(<0),(>0)]<*>) 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<0,y<0,x>0,y>0]!!d Try it online! Explanation The idea here is two-fold We notice that we ...

Top 50 recent answers are included