# Find out my number neighbors

The input consists of i rows with neighbors information. Each ith row contains 4 values, representing the neighbor of i to the North, East, South and West directions, respectively. So each value represents a neighbor at the given direction of the ith row, starting from row 1, and can go up to 65,535 rows. The 0 value indicates no neighbor to that direction.

For instance, if the first row is "0 2 3 10" it means that the i neighbor has three other neighbors: no one to the north, neighbor 2 to the east, neighbor 3 to the south and neighbor 10 to the west.

You need to output the array of neighbors, starting from the value which is most to the northwest. Each neighbor will be displayed only once, at its position relative to others. Let's see some examples:

Input:

0 0 0 0


No neighbors (empty case), output:

1


Input:

0 2 0 0
0 0 0 1


1 has neighbor 2 to the east. 2 has neighbor 1 to the west

Output:

1 2


Input:

0 2 0 0
0 0 3 1
2 0 0 0


1 has neighbor 2 to the east. 2 has neighbor 1 to the west and 3 to the south. 3 has neighbor 2 to the north

Output:

1 2
3


Input:

2 0 0 0
0 0 1 0


Output:

2
1


Input:

0 2 3 0
0 0 4 1
1 4 0 0
2 0 0 3


Output:

1 2
3 4


Rules:

• Test cases are separated by one empty line. Output of different test cases must also be separated by one empty line.
• The output graph is always connected. You are not going to have 1 neighbor to 2 only, along with 3 neighbor to 4 only (isolated from 1-2 component).
• All entries are valid. Example of invalid entries:
• Entries containing letters or any symbol different than spaces, line breaks and digits (0-9).
• the ith row containing the ith value (because one can't be its own neighbor).
• a negative value or value higher than 65,535.
• Less than four values in a row.
• More than four values in a row.
• The same neighbor pointing to two different directions (ex: 0 1 1 0).

Standard loopholes apply, and the shortest answer in bytes wins.

• Test cases are separated by one empty line - This is an unusual requirement. Normally it is given that challenge entries will handle one testcase at a time (one per invocation). If challenge entries can handle more than one testcase at a time, then great, but there is little value to strictly specifying how multiple testcase entries should be formatted. – Digital Trauma Apr 24 '18 at 18:48
• @Chaotic you can remove it completely (if you want to), the revision history take care of the changelog – Rod Apr 24 '18 at 19:06
• I don't understand how the output relates to the output. Can you explain in more detail what "array of neighbors" means, and by which rules this array should be created by? – Stewie Griffin Apr 24 '18 at 19:17
• Aaaaah, I think I get it. The neighbors are enumerated 1,2,.... I though they had a neighbor 2 "units" to the east, and 1 "unit" to the south and so on. Couldn't make sense of it. – Stewie Griffin Apr 24 '18 at 19:36
• @StewieGriffin yep I had to read through it a few times before it was clear – Digital Trauma Apr 24 '18 at 19:41

# Python 2, 152 bytes

l=input()
def f(x,y,n):
if m[x][y]<n:m[x][y]=n;[f(i%3-1+x,i/3-1+y,h)for h,i in zip(l[n-1],[3,7,5,1])]
e=len(l)
m=eval([[0]*e*2]*e*2)
f(e,e,1)
print m


Try it online!

The input order is NESW
f is a recursive function to populate the houses

• Sorry, it is not working. I even tried with your input format (which I guess it's fine): [[0, 5, 2, 0], [1, 6, 3, 0], [2, 7, 4, 0], [3, 8, 0, 0], [0, 9, 6, 1], [5, 10, 7, 2], [6, 11, 8, 3], [7, 12, 0, 4], [0, 13, 10, 5], [9, 14, 11, 6], [10, 15, 12, 7], [11, 16, 0, 8], [0, 17, 14, 9], [13, 18, 15, 10], [14, 19, 16, 11], [15, 20, 0, 12], [0, 21, 18, 13], [17, 22, 19, 14], [18, 23, 20, 15], [19, 24, 0, 16], [0, 0, 22, 17], [21, 0, 23, 18], [22, 0, 24, 19], [23, 0, 0, 20]] – Chaotic Apr 24 '18 at 19:59
• @Chaotic seems fine to me maybe the output format is not satisfactory? – Rod Apr 24 '18 at 21:47
• Ok, I got lost with all those zeros there. I think it woud be better to remove them, but I'm not used to code golf standard rules. – Chaotic Apr 24 '18 at 22:00
• Do you just remain the unused space? – l4m2 Apr 25 '18 at 1:33
• still golfing :)

# JavaScript (Node.js), 135 bytes

R=>R.map((t,y)=>r.map((T,Y)=>T.map((X,I)=>X==y+1?[-1,1,1,-1].map((x,i)=>t[i]?(r[a=Y+x*-~i%2]=r[a]||[])[I+x*i%2]=t[i]:0):0)),r=[[1]])&&r


Try it online!

Second approach

# JavaScript (Node.js), 130 bytes

f=(R,x=0,y=0,c=1,r=[[]])=>[-1,1,1,-1].map((d,i)=>(t=R[c-1][i])&&!(r[Y=y+d*-~i%2]=r[Y]||[])[X=x+d*i%2]?f(R,X,Y,t,r):0,r[y][x]=c)&&r


Try it online!