Skip to content

Commit

Permalink
Merge pull request #173 from cs0x7f/master
Browse files Browse the repository at this point in the history
Update threephase package to be way faster at generating pruning tables.
  • Loading branch information
jfly committed Aug 8, 2014
2 parents 5a1e83c + 7feb0a5 commit a978614
Show file tree
Hide file tree
Showing 15 changed files with 1,100 additions and 863 deletions.

This file was deleted.

6 changes: 5 additions & 1 deletion scrambles/src/scrambles.gwt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<exclude name="MainProgram.java"/>
<exclude name="test.java"/>
</source>
<source path='cs/threephase'>
<exclude name="Tools.java"/>
<exclude name="test.java"/>
</source>
<source path='net/gnehzr/tnoodle/utils'>
<exclude name="Utils.java"/>
<exclude name="GsonUtils.java"/>
Expand All @@ -47,7 +51,7 @@
<source path='net/gnehzr/tnoodle/svglite'/>

<source path='puzzle'>
<exclude name="FourByFourCubePuzzle.java"/>
<!-- <exclude name="FourByFourCubePuzzle.java"/> -->
</source>
<source path='net/gnehzr/tnoodle/scrambles'>
<exclude name="PuzzlePlugins.java"/>
Expand Down
130 changes: 68 additions & 62 deletions threephase/src/cs/threephase/Center1.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,80 +15,74 @@
*/

final class Center1 {

static int[][] ctsmv = new int[15582][36];
static int[] sym2raw = new int[15582];
static byte[] csprun = new byte[15582];

static int[][] symmult = new int[48][48];
static int[][] symmove = new int[48][36];
static int[] syminv = new int[48];
static int[] finish = new int[48];

static int[] raw2sym;

static void initSym2Raw() {
initSym();
Center1 c = new Center1();
c.set(0);
for (int i=0; i<48; i++) {
finish[syminv[i]] = c.get();
c.rot(0);
if (i%2==1) c.rot(1);
if (i%8==7) c.rot(2);
if (i%16==15) c.rot(3);
}

int[] occ = new int[735471/32+1];
int count = 0;
for (int i=0; i<735471; i++) {
if ((occ[i>>>5]&(1<<(i&0x1f))) == 0) {
sym2raw[count++] = i;
// occ[i] = true;
c.set(i);
for (int j=0; j<48; j++) {
int idx = c.get();
occ[idx>>>5] |= (1<<(idx&0x1f));
if (raw2sym != null) {
raw2sym[idx] = count << 6 | syminv[j];
}
c.rot(0);
if (j%2==1) c.rot(1);
if (j%8==7) c.rot(2);
if (j%16==15) c.rot(3);
}
sym2raw[count++] = i;
}
}
occ = null;
System.gc();
assert count == 15582;
}

static void init() {

initSym2Raw();
static void createPrun() {
Arrays.fill(csprun, (byte)-1);
csprun[0] = 0;
int depth = 0;
int done = 1;

while (done != 15582) {
boolean inv = depth > 4;
int select = inv ? -1 : depth;
int check = inv ? depth : -1;
depth++;
for (int i=0; i<15582; i++) {
if (csprun[i] == select) {
for (int m=0; m<27; m++) {
int idx = ctsmv[i][m] >>> 6;
if (csprun[idx] == check) {
++done;
if (inv) {
csprun[i] = (byte) depth;
break;
} else {
csprun[idx] = (byte) depth;
}
}
if (csprun[i] != select) {
continue;
}
for (int m=0; m<27; m++) {
int idx = ctsmv[i][m] >>> 6;
if (csprun[idx] != check) {
continue;
}
++done;
if (inv) {
csprun[i] = (byte) depth;
break;
} else {
csprun[idx] = (byte) depth;
}
}
}
// System.out.println(String.format("%2d%10d", depth, done));
}

}

static void createMoveTable() {
Expand All @@ -104,51 +98,51 @@ static void createMoveTable() {
}
}
}

byte[] ct = new byte[24];

Center1() {
for (int i=0; i<8; i++) {
ct[i] = 1;
}
for (int i=8; i<24; i++) {
ct[i] = 0;
}
}
}

Center1(byte[] ct) {
for (int i=0; i<24; i++) {
this.ct[i] = ct[i];
}
}

Center1(CenterCube c, int urf) {
for (int i=0; i<24; i++) {
this.ct[i] = (byte) ((c.ct[i]/2 == urf) ? 1 : 0);
}
}

void move(int m) {
int key = m % 3;
m /= 3;
switch (m) {
case 0: //U
swap(ct, 0, 1, 2, 3, key);
swap(ct, 0, 1, 2, 3, key);
break;
case 1: //R
swap(ct, 16, 17, 18, 19, key);
swap(ct, 16, 17, 18, 19, key);
break;
case 2: //F
swap(ct, 8, 9, 10, 11, key);
swap(ct, 8, 9, 10, 11, key);
break;
case 3: //D
swap(ct, 4, 5, 6, 7, key);
swap(ct, 4, 5, 6, 7, key);
break;
case 4: //L
swap(ct, 20, 21, 22, 23, key);
swap(ct, 20, 21, 22, 23, key);
break;
case 5: //B
swap(ct, 12, 13, 14, 15, key);
swap(ct, 12, 13, 14, 15, key);
break;
case 6: //u
swap(ct, 0, 1, 2, 3, key);
Expand Down Expand Up @@ -179,10 +173,10 @@ void move(int m) {
swap(ct, 12, 13, 14, 15, key);
swap(ct, 1, 20, 7, 18, key);
swap(ct, 0, 23, 6, 17, key);
break;
break;
}
}

}
void set(int idx) {
int r = 8;
for (int i=23; i>=0; i--) {
Expand All @@ -193,8 +187,8 @@ void set(int idx) {
}
}
}

int get() {
int get() {
int idx = 0;
int r = 8;
for (int i=23; i>=0; i--) {
Expand All @@ -206,6 +200,9 @@ int get() {
}

int getsym() {
if (raw2sym != null) {
return raw2sym[get()];
}
for (int j=0; j<48; j++) {
int cord = raw2sym(get());
if (cord != -1)
Expand All @@ -222,14 +219,14 @@ int getsym() {
static int raw2sym(int n) {
int m = Arrays.binarySearch(sym2raw, n);
return (m>=0 ? m : -1);
}
}

void set(Center1 c) {
for (int i=0; i<24; i++) {
this.ct[i] = c.ct[i];
}
}

void rot(int r) {
switch (r) {
case 0:
Expand All @@ -253,8 +250,8 @@ void rot(int r) {
move(dx3);
move(fx1);
move(bx3);
break;
}
break;
}
}
/*
0 I
Expand Down Expand Up @@ -282,8 +279,8 @@ void rot(int r) {
38 yz2
39 y'z2
*/
static String[] rot2str = {"", "y2", "x", "x y2", "x2", "z2", "x'", "x' y2", "", "", "", "", "", "", "", "",
"y z", "y' z'", "y2 z", "z'", "y' z", "y z'", "z", "z y2", "", "", "", "", "", "", "", "",
static String[] rot2str = {"", "y2", "x", "x y2", "x2", "z2", "x'", "x' y2", "", "", "", "", "", "", "", "",
"y z", "y' z'", "y2 z", "z'", "y' z", "y z'", "z", "z y2", "", "", "", "", "", "", "", "",
"y' x'", "y x", "y'", "y", "y' x", "y x'", "y z2", "y' z2", "", "", "", "", "", "", "", ""};


Expand All @@ -295,7 +292,7 @@ void rotate(int r) {
if (j%16==15) rot(3);
}
}

static int getSolvedSym(CenterCube cube) {
Center1 c = new Center1(cube.ct);
for (int j=0; j<48; j++) {
Expand All @@ -316,7 +313,7 @@ static int getSolvedSym(CenterCube cube) {
}
return -1;
}

public boolean equals(Object obj) {
if (obj instanceof Center1) {
Center1 c = (Center1)obj;
Expand All @@ -329,7 +326,7 @@ public boolean equals(Object obj) {
}
return false;
}

static void initSym() {
Center1 c = new Center1();
for (byte i=0; i<24; i++) {
Expand All @@ -338,7 +335,7 @@ static void initSym() {
Center1 d = new Center1(c.ct);
Center1 e = new Center1(c.ct);
Center1 f = new Center1(c.ct);

for (int i=0; i<48; i++) {
for (int j=0; j<48; j++) {
for (int k=0; k<48; k++) {
Expand All @@ -350,7 +347,7 @@ static void initSym() {
}
d.rot(0);
if (k%2==1) d.rot(1);
if (k%8==7) d.rot(2);
if (k%8==7) d.rot(2);
if (k%16==15) d.rot(3);
}
c.rot(0);
Expand All @@ -363,7 +360,7 @@ static void initSym() {
if (i%8==7) c.rot(2);
if (i%16==15) c.rot(3);
}

for (int i=0; i<48; i++) {
c.set(e);
c.rotate(syminv[i]);
Expand All @@ -381,5 +378,14 @@ static void initSym() {
}
}
}
}

c.set(0);
for (int i=0; i<48; i++) {
finish[syminv[i]] = c.get();
c.rot(0);
if (i%2==1) c.rot(1);
if (i%8==7) c.rot(2);
if (i%16==15) c.rot(3);
}
}
}
Loading

0 comments on commit a978614

Please sign in to comment.