5
\$\begingroup\$

Related:

Challenge:

Given a grid, with an ID starting at the center and spiraling out, what is the ID given a position in the fewest number of bytes?

Grid:

+---------------+---------------+---------------+---------------+---------------+
| id:  20       | id:  19       | id:  18       | id:  17       | id:  16       |
| pos: (-2, -2) | pos: (-1, -2) | pos: (0, -2)  | pos: (1, -2)  | pos: (2, -2)  |
+---------------+---------------+---------------+---------------+---------------+
| id:  21       | id:  6        | id:  5        | id:  4        | id:  15       |
| pos: (-2, -1) | pos: (-1, -1) | pos: (0, -1)  | pos: (1, -1)  | pos: (2, -1)  |
+---------------+---------------+---------------+---------------+---------------+
| id:  22       | id:  7        | id:  0        | id:  3        | id:  14       |
| pos: (-2, 0)  | pos: (-1, 0)  | pos: (0, 0)   | pos: (1, 0)   | pos: (2, 0)   |
+---------------+---------------+---------------+---------------+---------------+
| id:  23       | id:  8        | id:  1        | id:  2        | id:  13       |
| pos: (-2, 1)  | pos: (-1, 1)  | pos: (0, 1)   | pos: (1, 1)   | pos: (2, 1)   |
+---------------+---------------+---------------+---------------+---------------+
| id:  24       | id:  9        | id:  10       | id:  11       | id:  12       |
| pos: (-2, 2)  | pos: (-1, 2)  | pos: (0, 2)   | pos: (1, 2)   | pos: (2, 2)   |
+---------------+---------------+---------------+---------------+---------------+

Tests:

f(0, 0) = 0
f(1, 1) = 2
f(0, -1) = 5
f(2, 0) = 14
f(-2, -2) = 20

f(x, y) = id

Notes:

  • Grid will always be square (height == width)
  • Grid can be infinite in size
\$\endgroup\$
  • \$\begingroup\$ How can the grid be both square and infinite in size? \$\endgroup\$ – Jonathan Frech Mar 27 '18 at 23:00
  • \$\begingroup\$ Sorry, just trying to say it can be bigger than the example grid provided. \$\endgroup\$ – Justin808 Mar 27 '18 at 23:02
  • \$\begingroup\$ Sorta odd you chose to make the bottom right 1,1 and the top left -1,-1. Then again I guess Java does this in java.awt.* \$\endgroup\$ – Magic Octopus Urn Mar 27 '18 at 23:04
  • \$\begingroup\$ @MagicOctopusUrn - makes you think a little more? It's just the grid setup I've been using in my codebase. I didn't want to use the same old grid in my game. \$\endgroup\$ – Justin808 Mar 27 '18 at 23:06
  • 2
    \$\begingroup\$ The last case should output 20, not 24 \$\endgroup\$ – Luis Mendo Mar 27 '18 at 23:46
1
\$\begingroup\$

JavaScript (ES6), 64 bytes

Takes input in currying syntax (x)(y).

x=>y=>(d=(A=Math.abs)(A(x)-A(y))+A(x)+A(y))*d+(d-x+y)*(-y>x||-1)

Try it online!

Heavily inspired by this answer from math.stackexchange.com.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 58 bytes

y=>x=>(m=Math.max)(4*x*x-m(x+y,3*x-y),4*y*y+m(-x-y,x-3*y))

Try it online!

Use the formula from my previous answer, with some edit (because Javascript doesn't vectorize).

I came up with the formula myself. Basically, knowing that the formula must be quadratic in each 1/4 grid section, I find out the formula mostly by trial-and-error and then find a way to fit those together.

\$\endgroup\$
0
\$\begingroup\$

MATL, 15 bytes

|sQtE1YLqwG+X{)

Try it online! Or verify all test cases.

\$\endgroup\$
0
\$\begingroup\$

J, 37 bytes

(g@[-+<.-+[+[)>.(g=:4**:)@]-+>.]+]+-~

Try it online!

My first J answer! Use the formula from my Javascript answer.

  1. Precedence rules are weird. (at least I don't need to memorize too much, but it's counterintuitive)
  2. More than half of the characters are BF keywords.
  3. -+> and :).
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.