# Four Spiraling Axes

Take the numbers 0, 1, 2, 3, 4, ... and arrange them in a clockwise spiral, starting downward, writing each digit in its own separate square.

Then, given one of four distinct and consistent ASCII characters (your choice) representing an axis, and an input integer n, output the first n terms of the sequence described by selecting squares along the corresponding axis.

For example, below is the arranged spiral up to partway through 29. Suppose we use u / d / l / r for our four characters, representing up / down / left / right. Then, given u as input, we output 0, 5, 1, 4 ... (the positive y-axis) up to the nth term. If we were instead given l as input, then it would be 0, 3, 1, 1 ... up to the nth term.

  2---3---2---4---2---5---2
|                       |
2   1---3---1---4---1   6
|   |               |   |
2   2   4---5---6   5   2
|   |   |       |   |   |
1   1   3   0   7   1   7
|   |   |   |   |   |   |
2   1   2---1   8   6   2
|   |           |   |   |
0   1---0---1---9   1   8
|                   |   |
2---9---1---8---1---7   2


These are sequences on OEIS:

### Examples

d 19
[0, 1, 1, 8, 3, 7, 6, 2, 1, 5, 1, 1, 6, 2, 2, 1, 3, 4, 0]

r 72
[0, 7, 1, 7, 4, 2, 8, 1, 1, 3, 1, 2, 0, 2, 3, 1, 3, 4, 6, 5, 5, 5, 7, 7, 8, 8, 9, 6, 8, 1, 1, 1, 2, 3, 1, 8, 0, 6, 1, 7, 0, 9, 2, 8, 4, 3, 2, 1, 1, 7, 2, 6, 2, 1, 3, 3, 5, 5, 3, 2, 2, 0, 4, 3, 2, 5, 4, 6, 5, 0, 5, 1]

u 1



## Rules

• If applicable, you can assume that the input/output will fit in your language's native Integer type.
• If you're using integers to represent the four axes, you can use negative integers without breaking the rules.
• The input and output can be given by any convenient method.
• Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
• Standard loopholes are forbidden.
• This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
• Can we take negative integers for a couple directions? – mbomb007 Apr 17 '18 at 18:06
• @mbomb007 Sure, that would be OK. – AdmBorkBork Apr 17 '18 at 18:17
• Doesn't seem very practical, but can we return an integer that has the required digits except the leading zero (which would be implied anyway)? – Erik the Outgolfer Apr 17 '18 at 18:42
• @AdmBorkBork Basically I don't have such a solution in mind, just thought to ask. :) – Erik the Outgolfer Apr 17 '18 at 18:46
• A challenge is judged by the output. So if the zero isn't included, I'd think that would be wrong, because you'd count it wrong if the zero is excluded for string output. Python can output integers with leading zeros, so why not other languages. – mbomb007 Apr 17 '18 at 20:15

# Python 2, 948984837472 70 bytes

I used WolframAlpha and determined that an upper bound of 5n > 4n2+3n seems to be enough. It can be changed to 9n at no cost. For trying larger inputs, use 9*n*n instead of 5**n to avoid running out of memory.

lambda d,n:["".join(map(str,range(5**n)))[x*(4*x+d)]for x in range(n)]


Try it online!

The inputs for directions are:

• 3: right
• -3: down
• -1: left
• 1: up

Saved 14 bytes thanks to Rod
Saved 2 bytes thanks to Jonathan Allan

# MATL, 32 bytes

tE1YLtn:qVXzw)3Lt3\$)iX!w:qyfYm+)


Input is n, a, where a represents the axis as follows:

• 0: left;
• 1: up;
• 2: right;
• 3: down.

Output is a string.

# Jelly,  19  18 bytes

Uses the 5n trick from mbomb007's Python answer

4,0jḅɗ@€ị5*D€FʋṖ0;


A dyadic link taking n on the left and d and integer from: [-3,-1,1,3]:[v,<,^,>]

Try it online!

A 20 byte alternative that is both much faster and does not segfault for such small n is:

²×5D€ƲFị@4,0jḅɗ@€Ṗ0;


Try it online!

### How?

4,0jḅɗ@€ị5*D€FʋṖ0; - Link: integer, n; integer, d
ɗ@€           - last three links as a dyad with sw@pped arguments for €ach (of implicit range [1,n])
4,0                -   literal list [4,0]
j               -   join with d = [4,d,0]
ḅ              -   convert from base n = 4*n^2+d*n+0
ị          - index into...
5         -   five
*        -   exponentiate = 5^n
D€      -   decimal list of each (in implicit range [1,5^n])
F      -   flatten into a single list of the digits
Ṗ   - pop (drop the final element)
0; - prepend a zero


will work for considerable n (like +1000)

# JavaScript (Node.js), 104 bytes

f=(d,n)=>--n?[...f(d,n),C(n*(4*n+d))]:
C=(n,N=i=0)=>n>N?C(n-N,(p=10**i)*9*++i):+((p+--n/i|0)+"")[n%i]


Try it online!

### Explanation

• 3: right
• -3: down (-3 is legit according to comments)
• -1: left
• 1: up (like @mbomb007)

C- nth digit of Champernowne's constant

## ________________________________________________________

Less efficient method (wont work for 1000+)

# JavaScript (Node.js), 81 bytes

f=(d,n)=>eval(for(r=[],s=i=I="";I<n;)(s+=i++)[u=I*(4*I+d)]&&r.push(s[I++,u]),r)


Try it online!

# Stax, 13 bytes

â╞ê←τ"(]]⌐┘?N


Run and debug it

It takes input with the direction, followed by the count. Right, up, left, and down are 1, 3, 5, and 7 respectively. It takes a full minute to run the three provided test cases.