# Return Spiral Indexes!

Spirals are cool , so are numbers, and number spirals are even cooler. But what if I want a specific number, in a specific place using xy coordinates...

Challenge

Using an infinite spiral moving down first, curling in a counterclockwise manner

ex:

 6  5  4
7  0  3
8  1  2
9 10 11

1. Take negative and positive inputs(integers) in the form of (x, y)
2. When given those coordinates return the number at that position in the spiral
3. The integer inputs must be the maximum that your language can use

ex:

 6  5  4
7  0  3
8  1  2
9 10 11

input : (0,0)
output : 0

input : (1,1)
output : 4

input : (-1,-1)
output : 8

input : (5,5)
output : 100

input : (-5, -6)
output : 121

input : (2, -5)
output : 87

input : (-9, 2)
output : 349

input : (-7, 8)
output : 271

input : (-5, -7)
output : 170


Scores

This is a codegolf, so smallest code wins!!!

• This challenge has created the spiral tag. If we want to really use it (I'm not sure how useful it is), other questions about spirals should be tagged as well – Luis Mendo Feb 15 at 17:19
• This is (almost) the reverse of this challenge. – AdmBorkBork Feb 15 at 17:24
• I would call that a duplicate. There's barely anything changing when using a spiral starting downwards or rightwards. – Olivier Grégoire Feb 15 at 19:38
• @OlivierGrégoire The other one requires that you output the position, this challenge is the reverse, you need to output the number at the coordinates given – KrystosTheOverlord Feb 15 at 19:39
• This is in need of a couple more test cases using higher indices. – Shaggy Feb 15 at 22:49

# JavaScript (ES7), 50 bytes

This is using a slightly modified version of the formula that I've used and explained in this post.

x=>y=>(i=4*(x*x>y*y?x:y)**2)-(x<y||-1)*(i**.5-x-y)


Try it online!

• Just checked the higher indices, this answer definitely works. – KrystosTheOverlord Feb 17 at 16:59

# 05AB1E, 18 15 bytes

·nàDtIO-I›·<*-


Port of @Arnauld's JavaScript (ES7) answer.
And -3 bytes thanks to @Arnauld as well.

My answer uses the formula:

$$T=\max((2*x)^2, (2*y)^2)$$

$$P=\begin{cases} 1&\text{if }y>x\\ -1&\text{if }y\le x \end{cases}$$

$$T - (\sqrt{T} - (x+y)) * P$$

Which saves bytes with some of 05AB1E's convenient builtins, in comparison to the similar but slightly different formula @Arnauld uses in his answer.

Explanation:

·                # Double both values in the (implicit) input-list
n               # Then square both values
à              # Take the maximum of the two
Dt            # Duplicate it, and take its square-root
IO-         # Subtract the sum of the input-list
I›      # Check if y is larger than x (1 if truthy; 0 if falsey)
·<    # Double it, and decrease it by 1 (1 if 1; -1 if 0)
*   # Multiply both
-  # Subtract them from each other (and output the result implicitly)

• Good explanation you got there, and good job at completing the challenge! :) – KrystosTheOverlord Feb 19 at 0:25

# Perl 5, 55 bytes

From @Arnauld's formula

/ /;$_=($i=2*max abs$,abs$')**2-($<$'||-1)*($i-$-\$')


TIO

# Jelly, 15 bytes

Ḥ²Ṁ©½_S×>/Ḥ’Ɗ®_


Try it online!

Port of @KevinCruijssen’s version of @Arnauld’s formula so be sure to upvote them.

• Nice golfing there, you are tied for the least bytes in this challenge with KevinCruijssen's – KrystosTheOverlord Mar 30 at 20:52

# Wolfram Language (Mathematica), 45 bytes

(For[i=j=0,ReIm@i!=#,i+=I^⌈2√++j⌉I];j)&
`

Try it online!

Generates the spiral up to the desired point