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:
T00 | T01 | … | T06 |
T10 | T11 | … | T16 |
… | |||
TA0 | TA1 | … | TA6 |
… | |||
TR0 | TR1 | … | TR6 |
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:
TR6 | TR0 | … | TR5 |
T06 | T00 | … | T05 |
… | |||
TQ6 | TQ0 | … | TQ5 |
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:
State | NN | WW | SS | EE |
---|---|---|---|---|
T00 | NULL | NULL | T10 | T01 |
T01 | NULL | T00 | T11 | T02 |
T02 | NULL | T01 | T12 | T03 |
T03 | NULL | T02 | T13 | T04 |
T04 | NULL | T03 | T14 | T05 |
T05 | NULL | T04 | T15 | T06 |
T06 | NULL | T05 | T16 | NULL |
T10 | T00 | NULL | T20 | T11 |
T11 | T01 | T10 | T21 | T12 |
T12 | T02 | T11 | T22 | T13 |
T13 | T03 | T12 | T23 | T14 |
T14 | T04 | T13 | T24 | T15 |
T15 | T05 | T14 | T25 | T16 |
T16 | T06 | T15 | T26 | NULL |
T20 | T10 | NULL | NULL | T21 |
T21 | T11 | T20 | NULL | T22 |
T22 | T12 | T21 | NULL | T23 |
T23 | T13 | T22 | NULL | T24 |
T24 | T14 | T23 | NULL | T25 |
T25 | T15 | T24 | NULL | T26 |
T26 | T16 | T25 | NULL | NULL |
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;
[[ column ]] |
---|
[[ value ]] |
Valid answer:
State | NN | WW | SS | EE |
---|---|---|---|---|
T00 | T20 | T06 | T10 | T01 |
T01 | T21 | T00 | T11 | T02 |
T02 | T22 | T01 | T12 | T03 |
T03 | T23 | T02 | T13 | T04 |
T04 | T24 | T03 | T14 | T05 |
T05 | T25 | T04 | T15 | T06 |
T06 | T26 | T05 | T16 | T00 |
T10 | T00 | T16 | T20 | T11 |
T11 | T01 | T10 | T21 | T12 |
T12 | T02 | T11 | T22 | T13 |
T13 | T03 | T12 | T23 | T14 |
T14 | T04 | T13 | T24 | T15 |
T15 | T05 | T14 | T25 | T16 |
T16 | T06 | T15 | T26 | T10 |
T20 | T10 | T26 | T00 | T21 |
T21 | T11 | T20 | T01 | T22 |
T22 | T12 | T21 | T02 | T23 |
T23 | T13 | T22 | T03 | T24 |
T24 | T14 | T23 | T04 | T25 |
T25 | T15 | T24 | T05 | T26 |
T26 | T16 | T25 | T06 | T20 |