Project

General

Profile

Download (7.63 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / TBDino4.java @ 398f2870

1 dbdb71d0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.objectlib.tablebases;
11
12 d6e2cf37 Leszek Koltunski
import org.distorted.objectlib.R;
13 3103c3c8 Leszek Koltunski
import org.distorted.objectlib.helpers.OperatingSystemInterface;
14 dbdb71d0 Leszek Koltunski
15
///////////////////////////////////////////////////////////////////////////////////////////////////
16
17 a76bb8f6 Leszek Koltunski
public class TBDino4 extends TBDino
18 dbdb71d0 Leszek Koltunski
{
19 d6e2cf37 Leszek Koltunski
  private static final int[][] SAME = {{0,3,7},{1,2,5},{4,8,9},{6,10,11}};
20 a76bb8f6 Leszek Koltunski
  private static final int SOLVED1 =  6237;  // partition 011021302233
21
  private static final int SOLVED2 = 10837;  // partition 001102133223
22 dbdb71d0 Leszek Koltunski
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24
25
  public TBDino4()
26
    {
27
    super();
28
    }
29
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 3103c3c8 Leszek Koltunski
  public TBDino4(OperatingSystemInterface os)
33 dbdb71d0 Leszek Koltunski
    {
34 3103c3c8 Leszek Koltunski
    super(os, new int[] {R.raw.din4_3_pruning2,R.raw.din4_3_pruning3}, new int[] {R.raw.din4_3_pruning7});
35 dbdb71d0 Leszek Koltunski
    }
36
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
// specifically for the tablebase
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
// two solved positions: original and mirrored (left face swapped with right)
41
42
  @Override
43
  int[] getSolvedIndices()
44
    {
45 d6e2cf37 Leszek Koltunski
    return new int[] {SOLVED1,SOLVED2};
46 dbdb71d0 Leszek Koltunski
    }
47
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
// ditto
50
51
  @Override
52
  boolean isSolved(int index)
53
    {
54 d6e2cf37 Leszek Koltunski
    return index==SOLVED1 || index==SOLVED2;
55 dbdb71d0 Leszek Koltunski
    }
56
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58 a76bb8f6 Leszek Koltunski
// 15408 really (see https://www.jaapsch.net/puzzles/dinocube.htm)
59
// Here 15400 because we equal positions where colors are simply swapped (those are the same with
60
// respect to distance to the 2 solved positions) so there are (11 choose 2)*(8 choose 2)*(5 choose 2)
61
// = 55*28*10 = 15400 possibilities.
62
// We do not pack those tightly, some positions in the same orbit (by Burnside lemma) have 3 different
63
// indices.
64 dbdb71d0 Leszek Koltunski
65 a76bb8f6 Leszek Koltunski
  int getSize()
66 dbdb71d0 Leszek Koltunski
    {
67 a76bb8f6 Leszek Koltunski
    return 15400;
68 dbdb71d0 Leszek Koltunski
    }
69
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72 a76bb8f6 Leszek Koltunski
  int getMinScramble()
73 dbdb71d0 Leszek Koltunski
    {
74 a76bb8f6 Leszek Koltunski
    return 5;
75
    }
76 dbdb71d0 Leszek Koltunski
77 a76bb8f6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79
  int[] getMidPruningLevels()
80
    {
81
    return new int[] {2,3};
82 dbdb71d0 Leszek Koltunski
    }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86 a76bb8f6 Leszek Koltunski
  int[] getHighPruningLevels()
87 dbdb71d0 Leszek Koltunski
    {
88 a76bb8f6 Leszek Koltunski
    return new int[] {7};
89 dbdb71d0 Leszek Koltunski
    }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93 a76bb8f6 Leszek Koltunski
  int getGodsNumber()
94 dbdb71d0 Leszek Koltunski
    {
95 a76bb8f6 Leszek Koltunski
    return 7;
96 dbdb71d0 Leszek Koltunski
    }
97
98 d6e2cf37 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100
  private static int computeIndex(int[] partition, int section, int size)
101
    {
102
    int index0=0;
103
104
    for(;index0<12; index0++)
105
      if(partition[index0]==section ) break;
106
107
    int index1=index0+1;
108
    int D1 = 0;
109
110
    for(;index1<12; index1++)
111
      {
112
      int val = partition[index1];
113
      if(val >section ) D1++;
114
      if(val==section ) break;
115
      }
116
117
    int index2=index1+1;
118
    int D2 = 0;
119
120
    for(;index2<12; index2++)
121
      {
122
      int val = partition[index2];
123
      if(val >section ) D2++;
124
      if(val==section ) break;
125
      }
126
127
    return D1*size - (D1+1)*D1/2 + D2;
128
    }
129
130 dbdb71d0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132
  public static int indexFromPartition(int[] partition)
133
    {
134 d6e2cf37 Leszek Koltunski
    int index0 = computeIndex(partition,0,11);
135
    int index1 = computeIndex(partition,1,8);
136
    int index2 = computeIndex(partition,2,5);
137
138
    return index0 + 55*(index1 + 28*index2);
139 dbdb71d0 Leszek Koltunski
    }
140
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143 d6e2cf37 Leszek Koltunski
  private static void fillPart(int[] part, int index, int section, int size)
144 dbdb71d0 Leszek Koltunski
    {
145 d6e2cf37 Leszek Koltunski
    int index0=0;
146
147
    for(; index0<10; index0++)
148
      if( part[index0]<0 )
149
        {
150
        part[index0] = section;
151
        break;
152
        }
153
154
    int index1 = index0+1;
155
    int diff = size-1;
156
157
    for(; index1<11; index1++)
158
      if( part[index1]<0 )
159
        {
160
        if( index<diff )
161
          {
162
          part[index1] = section;
163
          break;
164
          }
165
        else { index-=diff; diff--; }
166
        }
167
168
    int index2 = index1+1;
169
170
    for(; index2<12; index2++)
171
      {
172
      if( part[index2]<0 ) index--;
173
      if( index<0 )
174
        {
175
        part[index2] = section;
176
        break;
177
        }
178
      }
179 dbdb71d0 Leszek Koltunski
    }
180
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
183 d6e2cf37 Leszek Koltunski
  private static int[] partitionFromIndex(int index)
184 dbdb71d0 Leszek Koltunski
    {
185 d6e2cf37 Leszek Koltunski
    int index0 = (index%55);
186
    index /= 55;
187
    int index1 = (index%28);
188
    int index2 = (index/28);
189
190
    int[] part = new int[12];
191
    for(int i=0; i<12; i++) part[i] = -1;
192 83dadb4a Leszek Koltunski
193 d6e2cf37 Leszek Koltunski
    fillPart(part,index0,0,11);
194
    fillPart(part,index1,1,8);
195
    fillPart(part,index2,2,5);
196
197
    for(int i=0; i<12; i++)
198
      if( part[i]<0 ) part[i] = 3;
199
200
    return part;
201 dbdb71d0 Leszek Koltunski
    }
202
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205 d6e2cf37 Leszek Koltunski
  private static void fillPerm(int[] perm, int[] part, int section)
206 dbdb71d0 Leszek Koltunski
    {
207 d6e2cf37 Leszek Koltunski
    int num=0;
208
    int[] same = SAME[section];
209
210
    for(int i=0; i<12; i++)
211
      if( part[i]==section )
212
        perm[same[num++]] = i;
213
    }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  private static int[] quatsFromPartition(int[] partition)
218
    {
219
    int[] perm = new int[12];
220
    fillPerm(perm,partition,0);
221
    fillPerm(perm,partition,1);
222
    fillPerm(perm,partition,2);
223
    fillPerm(perm,partition,3);
224
225
    return TBDino6.getQuatsFromPerm(perm);
226
    }
227
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
230
  private static int[] partitionFromQuats(int[] quats)
231
    {
232
    int[] perm = new int[12];
233
    System.arraycopy(quats, 0, perm, 0, 12);
234
    TBDino6.getPermFromQuats(perm);
235
236
    int[] part = new int[12];
237
238
    for(int i=0; i<4; i++)
239
      {
240
      int[] index = SAME[i];
241
      part[perm[index[0]]] = part[perm[index[1]]] = part[perm[index[2]]] = (-i-1);
242
      }
243
244
    int newVal=0;
245
246
    for(int i=0; i<12; i++)
247
      {
248
      int val = part[i];
249
250
      if( val<0 )
251
        {
252
        for(int j=i; j<12; j++)
253
          if( part[j]==val ) part[j] = newVal;
254
255
        newVal++;
256
        }
257
      }
258
259
    return part;
260 dbdb71d0 Leszek Koltunski
    }
261
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263
264
  int[] getQuats(int index)
265
    {
266
    int[] partition = partitionFromIndex(index);
267
    return quatsFromPartition(partition);
268
    }
269
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
272
  int getIndex(int[] quats)
273
    {
274 0ed8f43f Leszek Koltunski
    normalizeQuats(quats);
275 dbdb71d0 Leszek Koltunski
    int[] partition = partitionFromQuats(quats);
276
    return indexFromPartition(partition);
277
    }
278
}