commit d8003f3a15e163bd2d522b1e8b12af31f7ab21e4
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Mon Feb 27 14:04:49 2023 +0100

    Pyraminx solver: progress

diff --git a/src/main/java/org/distorted/objectlib/tablebases/ImplementedTablebasesList.java b/src/main/java/org/distorted/objectlib/tablebases/ImplementedTablebasesList.java
index f286fe1e..2445bf00 100644
--- a/src/main/java/org/distorted/objectlib/tablebases/ImplementedTablebasesList.java
+++ b/src/main/java/org/distorted/objectlib/tablebases/ImplementedTablebasesList.java
@@ -23,6 +23,7 @@ public enum ImplementedTablebasesList
   PYRAMINX_DUO  (ObjectType.PDUO_2, TablebasesPyraminxDuo.class),
   IVY_CUBE      (ObjectType.IVY_2 , TablebasesIvyCube.class),
   CU_232        (ObjectType.CU_232, TablebasesCuboid232.class),
+  PYRA_3        (ObjectType.PYRA_3, TablebasesPyraminx.class),
   ;
 
   public static final int NUM_OBJECTS = values().length;
diff --git a/src/main/java/org/distorted/objectlib/tablebases/TablebasesPyraminx.java b/src/main/java/org/distorted/objectlib/tablebases/TablebasesPyraminx.java
index 914c6f03..bdcf5d1a 100644
--- a/src/main/java/org/distorted/objectlib/tablebases/TablebasesPyraminx.java
+++ b/src/main/java/org/distorted/objectlib/tablebases/TablebasesPyraminx.java
@@ -21,6 +21,16 @@ import org.distorted.objectlib.objects.TwistyPyraminx;
 
 public class TablebasesPyraminx extends TablebasesAbstract
 {
+  public static final int[][] EDGE_QUATS = new int[][]
+      {
+          { 0,10},  // even-odd quat (i.e. even-odd twist of the relevant edge piece)
+          { 1, 7},
+          { 4, 8},
+          { 2, 6},
+          { 3, 5},
+          { 9,11}
+      };
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   public TablebasesPyraminx()
@@ -62,10 +72,10 @@ public class TablebasesPyraminx extends TablebasesAbstract
              { 0.5f, SQ2/4, 0.0f},
 
              { 0.0f,-SQ2/2, 0.0f},
-             {-0.5f,  0.0f, 0.5f},
-             {-0.5f,  0.0f,-0.5f},
-             { 0.5f,  0.0f, 0.5f},
              { 0.5f,  0.0f,-0.5f},
+             { 0.5f,  0.0f, 0.5f},
+             {-0.5f,  0.0f,-0.5f},
+             {-0.5f,  0.0f, 0.5f},
              { 0.0f, SQ2/2, 0.0f},
          };
     }
@@ -97,23 +107,140 @@ public class TablebasesPyraminx extends TablebasesAbstract
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  public static void getEdgePermutation(int[] output, int[] quats)
+  public static void getEdgePermutation(int[] output, int[] quats, int index)
+    {
+    for(int i=0; i<6; i++ )
+      {
+      int quat = quats[i+index];
+
+      for(int j=0; j<6; j++)
+        {
+        int[] edge = EDGE_QUATS[j];
+        if( quat==edge[0] || quat==edge[1] ) { output[i]=j; break; }
+        }
+      }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  public static void getEdgeTwist(int[] output, int[] quats, int index)
     {
+    for(int i=0; i<6; i++)
+      {
+      int quat = quats[i+index];
+
+      for(int j=0; j<6; j++)
+        {
+        int[] edge = EDGE_QUATS[j];
+        if( quat==edge[0] ) { output[i]=0; break; }
+        if( quat==edge[1] ) { output[i]=1; break; }
+        }
+      }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
 
+  private void getEdgeQuats(int twist, int[] perm, int[] output)
+    {
+    for(int i=0; i<6; i++)
+      {
+      int p = perm[i];
+      int t = twist%2;
+      twist/=2;
+      output[i] = EDGE_QUATS[p][t];
+      }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private int getVertexTwist(int[] quats)
+    {
+    int twist = 0;
+
+    if( quats[3]==5 ) twist += 1;
+    if( quats[3]==6 ) twist += 2;
+    twist *= 3;
+    if( quats[2]==7 ) twist += 1;
+    if( quats[2]==8 ) twist += 2;
+    twist *= 3;
+    if( quats[1]==1 ) twist += 1;
+    if( quats[1]==2 ) twist += 2;
+    twist *= 3;
+    if( quats[0]==3 ) twist += 1;
+    if( quats[0]==4 ) twist += 2;
+
+    return twist;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private void getVertexQuats(int twist, int[] output)
+    {
+    switch(twist%3)
+      {
+      case 0: output[0]=0; break;
+      case 1: output[0]=3; break;
+      case 2: output[0]=4; break;
+      }
+    twist /= 3;
+
+    switch(twist%3)
+      {
+      case 0: output[1]=0; break;
+      case 1: output[1]=1; break;
+      case 2: output[1]=2; break;
+      }
+    twist /= 3;
+
+    switch(twist%3)
+      {
+      case 0: output[2]=0; break;
+      case 1: output[2]=7; break;
+      case 2: output[2]=8; break;
+      }
+    twist /= 3;
+
+    switch(twist%3)
+      {
+      case 0: output[3]=0; break;
+      case 1: output[3]=5; break;
+      case 2: output[3]=6; break;
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   int[] getQuats(int index)
     {
-    return new int[] {0,0,0,0, 0,0,0,0,0,0};
+    int vertexTwist = (index%81);
+    index /= 81;
+    int edgeTwist = (index%32);
+    int perm_num = index/32;
+
+    int[] quats = new int[10];
+
+    getVertexQuats(vertexTwist,quats);
+    int[] permutation = new int[6];
+    TablebaseHelpers.getPermutationFromNum(permutation,6,perm_num);
+    getEdgeQuats(edgeTwist,permutation,quats);
+
+    return quats;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   int getIndex(int[] quats)
     {
-    return 0;
+    int vertexTwist = getVertexTwist(quats);
+    int[] permutation = new int[6];
+    getEdgePermutation(permutation,quats,4);
+    int[] twist = new int[6];
+    getEdgeTwist(twist,quats,4);
+
+    int edgeTwist = twist[0]+ 2*(twist[1]+ 2*(twist[2]+ 2*(twist[3]+ 2*twist[4])));
+    int perm_num = TablebaseHelpers.computeEvenPermutationNum(permutation);
+
+    return vertexTwist + 81*(edgeTwist + 32*perm_num);
     }
 }  
 
