Torus Planet

Planet Torus has toroidal surface where torussians reside. If one projects it on the plane with periodic cylinder Mercadot projection then Torus appears as matrix with seven columns and their cells will be states. According to 28 decimal numeral system accepted on the planet, states are named in the style:

T00T01T06
T10T11T16
TA0TA1TA6
TR0TR1TR6

Mercadot was a famous traveler and cartographer, who resided in state T00, and was discoverer of planet’s map, which minimally distorts angles and distances. All states on Torus are approximately equivalent from topology point of view, so Mercadot developed atlas for all the Torus’s states. For example, the sketch map for the state TR6 look likes as follows:

TR6TR0TR5
T06T00T05
TQ6TQ0TQ5

As a peculiarity of toroidal surface, it is necessary to remark that the state T00, like any other Torus state, borders on with 8 states, not with 3 states as one can think before.

Uniform abbreviations are used for notation of neighbor states. For example, T00 habitants name TR0 as north neighbor (NN), TR6 as north-west neighbor (NW) and so on (if we go anticlockwise). Other states denote their neighbors in the same manner (NN, NW, WW, SW, SS, SE, EE, and NE). Possibly this is connected with direction of magnetic fields of the planet.

As a result of civilization development in torussians galaxy there was discovered lots resembled planets. Their names are specified in astronomic catalog in the following way: Torus MxN, where M is a number of regions by vertical (latitude), N is a number of regions by horizontal (longitude). Such planet has MxN number of base regions (or states as in main planet Torus). According to classification the planet Torus has notation Torus 28x7.

Until recently the biggest planet (by partition) from discovered planets was Torus 36x36. To describe its topology torussians invented hexatridecimal numerical system BASE 36. According to the theory, regions on the planet Torus 36x36 are enumerated from T00 to TZZ.

Recently, because of appearance the torussians inside sql-ex.ru site, much information about Lorus Galaxy became known. It turned out that Torus 36x36 is not biggest planet of the system.  In the aims of cultural exchange some facts of historic chronicles of Torus was added to common and checking site databases (Painting, Ships, etc.).

The sql-ex task situations explains that Torus unit of length is called a torometer, and united currency is torobucks.  The Torus life base is water and its ice variety. Task situation contains other information on physical-chemical characteristics, economical, social and political systems of planets Torus MxN.

Now we will not be tied with our “common” databases and present the next “universal” task situation on Torus.

Neighbor states on Torus 3x7

For each state of the Torus 3x7:

T00    T01    …    T06
T10    T11    …    T16
T20    T21    …    T26
find a list of neighbor states, which have bounds of not non-zero length. Result must be outputted in the table with five columns: [State], [NN], [WW], [SS], [EE].

Hint. This is the answer for common (non-toroidal) case:

StateNNWWSSEE
T00NULLNULLT10T01
T01NULLT00T11T02
T02NULLT01T12T03
T03NULLT02T13T04
T04NULLT03T14T05
T05NULLT04T15T06
T06NULLT05T16NULL
T10T00NULLT20T11
T11T01T10T21T12
T12T02T11T22T13
T13T03T12T23T14
T14T04T13T24T15
T15T05T14T25T16
T16T06T15T26NULL
T20T10NULLNULLT21
T21T11T20NULLT22
T22T12T21NULLT23
T23T13T22NULLT24
T24T14T23NULLT25
T25T15T24NULLT26
T26T16T25NULLNULL

This is solution for toroidal case:

select  
 'T'+cast(i  as varchar)+cast(j  as varchar) [State]  
,'T'+cast(k1 as varchar)+cast(j1 as varchar) [NN]  
,'T'+cast(i2 as varchar)+cast(k2 as varchar) [WW]  
,'T'+cast(k4 as varchar)+cast(j4 as varchar) [SS]  
,'T'+cast(i3 as varchar)+cast(k3 as varchar) [EE]  
from (select j=0 union all select 1 union all select 2 union all select 3 union all   
         select 4 union all select 5 union all select 6)J  
cross join (select i=0 union all select 1 union all select 2)I  
cross apply(select i1=i-1,j1=j,i2=i,j2=j-1,i3=i,j3=j+1,i4=i+1,j4=j)D  
cross apply(select  
 k1=case when i1<0 then 2 else i1 end  
,k2=case when j2<0 then 6 else j2 end  
,k3=case when j3>6 then 0 else j3 end  
,k4=case when i4>2 then 0 else i4 end  
)E;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]

Valid answer:

StateNNWWSSEE
T00T20T06T10T01
T01T21T00T11T02
T02T22T01T12T03
T03T23T02T13T04
T04T24T03T14T05
T05T25T04T15T06
T06T26T05T16T00
T10T00T16T20T11
T11T01T10T21T12
T12T02T11T22T13
T13T03T12T23T14
T14T04T13T24T15
T15T05T14T25T16
T16T06T15T26T10
T20T10T26T00T21
T21T11T20T01T22
T22T12T21T02T23
T23T13T22T03T24
T24T14T23T04T25
T25T15T24T05T26
T26T16T25T06T20