Clean, 284 279 272 262 bytes
import StdEnv
l=[0,-1,-1,0,1,1]
c(u,v)(p,q)=(u-p)^2+(v-q)^2<2||(u-p)*(q-v)==1
$[h:t]m=hd[[e: $t[(h,e):m]]\\e<-[1..]|and[e<>j\\(u,v)<-m|c h u,(p,q)<-m|q==v,(i,j)<-m|c p i]]
$(scan(\(a,b)(u,v)=(a-u,b-v))(0,0)[(i,j)\\n<-[1..],i<-[1,1:l]&j<-l,_<-[max(~j<<i)1..n]])[]
Try it online!
Generates the sequence forever.
Hexagon Mapping
Most of the code goes into mapping hexagons uniquely to (x,y)
coordinates so that there's a single, simple function to determine adjacency which holds for all point mappings.
The mapped points look like this:
---
--- < 2,-2> --- x-axis ___.X'
--- < 1,-2> === < 2,-1> --- /__.X'
< 0,-2> === < 1,-1> === < 2, 0>'
=== < 0,-1> === < 1, 0> ===
<-1,-1> === < 0, 0> === < 1, 1>
=== <-1, 0> === < 0, 1> ===
<-2, 0> === <-1, 1> === < 0, 2>.__
--- <-2, 1> === <-1, 2> --- \ 'Y.___
--- <-2, 2> --- y-axis 'Y.
---
From there, determining adjacency is trivial, and occurs when one of:
x1 == x2
and abs(y1-y2) == 1
y1 == y2
and abs(x1-x2) == 1
y1 == y2 - 1
and x2 == x1 - 1
y1 == y2 + 1
and x2 == x1 + 1
x1 == x2
and y1 == y2
Point Generation
Notice that when traversing the hexagon in a spiral the differences recur for each layer n
:
n
steps of (1,0)
n-1
steps of (1,-1)
n
steps of (0,-1)
n
steps of (-1,0)
n
steps of (-1,1)
n
steps of (0,1)
This generates the points in the right order by taking sums of prefixes of this sequence:
scan(\(a,b)(u,v)=(a-u,b-v))(0,0)[(i,j)\\n<-[1..],i<-[1,1:l]&j<-l,_<-[max(~j<<i)1..n]]
Bringing it Together
The code that actually finds the sequence from the question is just:
$[h:t]m=hd[[e: $t[(h,e):m]]\\e<-[1..]|and[e<>j\\(u,v)<-m|c h u,(p,q)<-m|q==v,(i,j)<-m|c p i]]
Which in turn is mostly filtering by and[r<>j\\(u,v)<-m|c h u,(p,q)<-m|q==v,(i,j)<-m|c p i]
This filter takes points from m
(the list of already-mapped points) by:
- Ignoring natural numbers that are equal to any
j
- For every
(i,j)
where i
is adjacent to p
- For every
(p,q)
where the value q
is equal to v
- For every
(u,v)
where u
is adjacent to the current point
I can't see the image as it's blocked here, so maybe I'm missing something, but your example shows a(11)=4, but in your sequence list a(11) is 5. – Geobits – 2018-12-12T20:55:33.883
Just a mistake—thanks for catching it. – Peter Kagey – 2018-12-12T20:57:04.583
7This OEIS sequence was submitted by yourself, apparently. Nice. :) – Arnauld – 2018-12-12T21:03:50.323
what is the limit for n ? is there a time limit ? – Setop – 2018-12-12T21:04:41.873
@Setop, you just have to write a program which computes the sequence correctly, and could determine an arbitrary value in the sequence given enough time/memory/etc. – Peter Kagey – 2018-12-12T21:12:45.623
5Awaiting Hexagony answer... – Jonathan Allan – 2018-12-13T19:51:34.620