public static int GetShortestPathLength(int iFirstValue, int iSecondValue) {
int[] firstResult = getXYZ(iFirstValue);
int[] secondResult = getXYZ(iSecondValue);
int i = Math.abs(firstResult[0]-secondResult[0]);
int j = Math.abs(firstResult[1]-secondResult[1]);
int k = Math.abs(firstResult[2]-secondResult[2]);
int x = i+j;
int y = i+k;
int z = j+k;
return (x<y?x:y)<z?(x<y?x:y):z;
}
public static int[] getXYZ(int value) {
if(value==1)
{
int [] result = {0,0,0};
return result;
}
int i = 0;
int sum = 1;
while (sum < value) {
sum += 6 * (++i);
}
int side = (sum - value) / i;
int step = (sum - value) % i;
int x = 0,y=0,z=0;
switch (side)
{
case 0: x = i; y = -i+step; z = x+y;break;
case 1: z = i; y = step; x = z-y;break;
case 2: y = i; z = i-step; x = z-y;break;
case 3: x = -i; y = i-step; z = x+y;break;
case 4: z = -i; y = -step; x = z-y;break;
case 5: y = -i; z = -i+step; x = z-y;break;
default: break;
}
int [] result = {x,y,z};
return result;
}
int[] firstResult = getXYZ(iFirstValue);
int[] secondResult = getXYZ(iSecondValue);
int i = Math.abs(firstResult[0]-secondResult[0]);
int j = Math.abs(firstResult[1]-secondResult[1]);
int k = Math.abs(firstResult[2]-secondResult[2]);
int x = i+j;
int y = i+k;
int z = j+k;
return (x<y?x:y)<z?(x<y?x:y):z;
}
public static int[] getXYZ(int value) {
if(value==1)
{
int [] result = {0,0,0};
return result;
}
int i = 0;
int sum = 1;
while (sum < value) {
sum += 6 * (++i);
}
int side = (sum - value) / i;
int step = (sum - value) % i;
int x = 0,y=0,z=0;
switch (side)
{
case 0: x = i; y = -i+step; z = x+y;break;
case 1: z = i; y = step; x = z-y;break;
case 2: y = i; z = i-step; x = z-y;break;
case 3: x = -i; y = i-step; z = x+y;break;
case 4: z = -i; y = -step; x = z-y;break;
case 5: y = -i; z = -i+step; x = z-y;break;
default: break;
}
int [] result = {x,y,z};
return result;
}