Created
July 12, 2022 21:50
-
-
Save btownrippleman/a8661443440ba61e7fb37fca05304d46 to your computer and use it in GitHub Desktop.
This code enumerates all possible configurations of a pocket cube rotating along each of the x, y and z axes rotating only in one direction.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "time" | |
| ) | |
| // parallelization := 4 | |
| var cubeMaps = map[string][]int {"x": {1, 2, 6, 5}, "y": {3,7,6,2}, "z": {0, 1, 2, 3}} | |
| var faces2rotatePairs = map[string]map[int]int { "x": {2:1,1:2,0:0}, "y": {2:0,0:2,1:1}, "z": {1:0,0:1,2:2}} | |
| var start = time.Now() | |
| var configurations = new([6000000][8][3]int) | |
| var hashArray = new([6000000]string) | |
| var initial_config = [8][3]int{{0, 1, 2}, {3, 1, 2}, {3, 5, 2}, {0, 5, 2}, {0, 1, 4}, {3, 1, 4}, {3, 5, 4}, {0, 5, 4}} | |
| var newConfig = initial_config | |
| var newHash = "" | |
| var exist = true | |
| var mapOfExistingConfigurations = make(map[string]string) | |
| var lo = 0 | |
| var hi = 1 | |
| var numrotations = 1 | |
| var newIndex = hi | |
| var axes = []string {"x","y","z"} | |
| var newConfigsProduced = true | |
| func main() { | |
| mapOfExistingConfigurations[genHash(initial_config)] = "0" | |
| hashArray[0] = genHash(initial_config) | |
| configurations[0] = initial_config | |
| for newConfigsProduced { | |
| newConfigsProduced = false | |
| // wg.Add(hi) | |
| checkEachConfiguration() | |
| newConfigsProduced = newConfigsProduced || newIndex !=hi | |
| fmt.Println(fmt.Sprint(numrotations), "rotations yields", fmt.Sprint(newIndex-hi), "new configurations computed in", time.Since(start)) | |
| lo = hi | |
| hi = newIndex | |
| numrotations += 1 | |
| } | |
| fmt.Println("time passed: ", time.Since(start)," with ", newIndex," different configurations total") | |
| } | |
| func checkEachConfiguration() map[string]string { | |
| for lo != hi { | |
| for _, axis := range axes { | |
| newConfig := rotate(configurations[lo], axis) | |
| newHash := genHash(newConfig) | |
| _, exist := mapOfExistingConfigurations[newHash] | |
| mapOfExistingConfigurations[newHash] = "0" //existingHash + axis + fmt.Sprint(direction) | |
| hashArray[newIndex] = newHash | |
| configurations[newIndex] = newConfig | |
| newIndex += B2i(!exist) | |
| } | |
| lo = lo + 1 | |
| } | |
| return mapOfExistingConfigurations | |
| } | |
| func B2i(b bool) int { | |
| if b { | |
| return 1 | |
| } | |
| return 0 | |
| } | |
| func rotate(inM [8][3]int, axis string ) [8][3]int { | |
| outM := inM | |
| cubeMap := cubeMaps[axis] | |
| faceMap := faces2rotatePairs[axis] | |
| outM[cubeMap[1]][faceMap[0]] = inM[cubeMap[0]][0]; | |
| outM[cubeMap[1]][faceMap[1]] = inM[cubeMap[0]][1]; | |
| outM[cubeMap[1]][faceMap[2]] = inM[cubeMap[0]][2]; | |
| outM[cubeMap[2]][faceMap[0]] = inM[cubeMap[1]][0]; | |
| outM[cubeMap[2]][faceMap[1]] = inM[cubeMap[1]][1]; | |
| outM[cubeMap[2]][faceMap[2]] = inM[cubeMap[1]][2]; | |
| outM[cubeMap[3]][faceMap[0]] = inM[cubeMap[2]][0]; | |
| outM[cubeMap[3]][faceMap[1]] = inM[cubeMap[2]][1]; | |
| outM[cubeMap[3]][faceMap[2]] = inM[cubeMap[2]][2]; | |
| outM[cubeMap[0]][faceMap[0]] = inM[cubeMap[3]][0]; | |
| outM[cubeMap[0]][faceMap[1]] = inM[cubeMap[3]][1]; | |
| outM[cubeMap[0]][faceMap[2]] = inM[cubeMap[3]][2] | |
| // for i:=0; i < 4; i++{ | |
| // for k:=0; k < 3; k++ { | |
| // outM[cubeMap[(i+1+4)%4]][faceMap[k]] = inM[cubeMap[i]][k] | |
| // } | |
| // } | |
| return outM | |
| } | |
| func genHash(arr [8][3]int) string { | |
| return fmt.Sprint(arr) | |
| // return fmt.Sprint(arr[0][0])+fmt.Sprint(arr[0][1])+fmt.Sprint(arr[0][2])+fmt.Sprint(arr[1][0])+fmt.Sprint(arr[1][1])+fmt.Sprint(arr[1][2])+fmt.Sprint(arr[2][0])+fmt.Sprint(arr[2][1])+fmt.Sprint(arr[2][2])+fmt.Sprint(arr[3][0])+fmt.Sprint(arr[3][1])+fmt.Sprint(arr[3][2])+fmt.Sprint(arr[4][0])+fmt.Sprint(arr[4][1])+fmt.Sprint(arr[4][2])+fmt.Sprint(arr[5][0])+fmt.Sprint(arr[5][1])+fmt.Sprint(arr[5][2])+fmt.Sprint(arr[6][0])+fmt.Sprint(arr[6][1])+fmt.Sprint(arr[6][2])+fmt.Sprint(arr[7][0])+fmt.Sprint(arr[7][1])+fmt.Sprint(arr[7][2]); | |
| // outString := ""; for i := range arr { for _, j:= range arr[i] { outString += fmt.Sprint(j)}} | |
| // return outString | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment