Guest User

All code for solver

a guest
Nov 26th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.20 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.PrintWriter;
  3. import java.util.Arrays;
  4. import java.util.HashSet;
  5. import java.util.Scanner;
  6. import java.util.Set;
  7. import java.util.Stack;
  8.  
  9.  
  10.  
  11. public class Cube {
  12.     private final int
  13.         TOP = 0,
  14.         LEFT = 1,
  15.         FRONT = 2,
  16.         RIGHT = 3,
  17.         BACK = 4,
  18.         BOTTOM = 5;
  19.  
  20.     private int [][] cube;
  21.  
  22.     public static void main(String[] args) {
  23.         Cube c = new Cube();
  24.         Cube.solve(c);
  25.      
  26.     }
  27.    
  28.     static private int [][]scanCube(int[][] cube){
  29.         String[] sides = { "TOP", "LEFT", "FRONT", "RIGHT", "BACK", "BOTTOM" };
  30.         int side_index = 0;
  31.         Scanner in = new Scanner(System.in);
  32.  
  33.         int[][] input = new int [6][4];
  34.         System.out.println("Welcome to the 2x2x2 cube solver :)\n \n"
  35.                 + "Please enter your cube");
  36.  
  37.         for (String side: sides) {
  38.             System.out.println("Enter the colors for the " + side + " side");
  39.             System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
  40.             int counter = 0;
  41.             while(counter < 4){
  42.                 input[side_index][counter] = Integer.parseInt(in.next());
  43.                 counter++;
  44.             }
  45.             side_index++;
  46.         }
  47.         in.close();
  48.        return input;
  49.     }
  50.    
  51.     static private void solve(Cube c) {
  52.        
  53.         Set<Cube> cubesFound = new HashSet<Cube>();
  54.         cubesFound.add(c);
  55.  
  56.         Stack<Cube> s = new Stack<Cube>();
  57.         s.push(c);
  58.  
  59.         Set<Stack<Cube>> initialPaths = new HashSet<Stack<Cube>>();
  60.         initialPaths.add(s);
  61.        
  62.         solve(initialPaths, cubesFound);
  63.     }
  64.  
  65.     static private void solve(Set<Stack<Cube>> livePaths, Set<Cube> cubesFoundSoFar) {
  66.         System.out.println("livePaths size:" + livePaths.size());
  67.         int numDupes = 0;
  68.  
  69.         Set<Stack<Cube>> newLivePaths = new HashSet<Stack<Cube>>();
  70.  
  71.         for(Stack<Cube> currentPath : livePaths) {
  72.  
  73.             Set<Cube> nextStates = currentPath.peek().getNextStates(cubesFoundSoFar);
  74.  
  75.             for (Cube next : nextStates) {
  76.                 if (currentPath.size() > 1 && next.isSolved()) {
  77.                     currentPath.push(next);
  78.                     System.out.println("Path length:" + currentPath.size());
  79.                     //This is the line that prints out the cube below
  80.                     System.out.println("Path:" + currentPath);
  81.                     System.exit(0);
  82.  
  83.                 } else if (!cubesFoundSoFar.contains(next)) {
  84.                     Stack<Cube> newCurrentPath = new Stack<Cube>();
  85.                     newCurrentPath.addAll(currentPath);
  86.                     newCurrentPath.push(next);
  87.                     newLivePaths.add(newCurrentPath);
  88.                     cubesFoundSoFar.add(next);
  89.                 } else {
  90.                     numDupes += 1;
  91.                 }
  92.             }
  93.         }
  94.         String storeStates = "positions.txt";
  95.         try {
  96.             PrintWriter outputStream = new PrintWriter(storeStates);
  97.             outputStream.println(cubesFoundSoFar);
  98.             outputStream.println(storeStates);
  99.             outputStream.close();
  100.            
  101.         } catch (FileNotFoundException e) {
  102.             // TODO Auto-generated catch block
  103.             e.printStackTrace();
  104.         }
  105.  
  106.         System.out.println("Duplicates found " + numDupes + ".");
  107.         solve(newLivePaths, cubesFoundSoFar);
  108.     }
  109.    
  110.    
  111.    
  112.     private Cube() {
  113.         cube = new int[][] {
  114.             { 0, 0, 0, 0 },
  115.             { 1, 1, 1, 1 },
  116.             { 2, 2, 2, 2 },
  117.             { 3, 3, 3, 3 },
  118.             { 4, 4, 4, 4 },
  119.             { 5, 5, 5, 5 }
  120.         };
  121.         cube = scanCube(cube);
  122.         cube = print_cube(cube);
  123.        
  124.     }
  125.    
  126.     private Cube(Cube other) {
  127.         cube = new int[other.cube.length][];
  128.         for (int i = 0; i < other.cube.length; i++) {
  129.             cube[i] = Arrays.copyOf(other.cube[i], other.cube[i].length);
  130.         }
  131.        
  132.     }
  133.  
  134.     private boolean isSolved() {
  135.         for (int i = 0; i < cube.length; i++) {
  136.             for (int k = 1; k < cube[i].length; k++) {
  137.                 if (cube[i][0] != cube[i][k]) {
  138.                     return false;
  139.                 }
  140.             }
  141.         }
  142.         return true;
  143.     }
  144.  
  145.    
  146.     @Override
  147.     public boolean equals(Object other) {
  148.        
  149.         return other instanceof Cube && Arrays.deepEquals(((Cube) other).cube, cube);
  150.     }
  151.  
  152.     @Override
  153.     public int hashCode() {
  154.         return Arrays.deepHashCode(cube);
  155.     }
  156.        
  157.    
  158.  
  159.     public Set<Cube> getNextStates(Set<Cube> cubesFoundSoFar) {
  160.           Set<Cube> s = new HashSet<Cube>();
  161.  
  162.           Cube c = new Cube(this);
  163.           c.rotateFrontCCW();
  164.           if (!cubesFoundSoFar.contains(c))
  165.             s.add(c);
  166.  
  167.           c = new Cube(this);
  168.           c.rotateFront2();
  169.           if (!cubesFoundSoFar.contains(c))
  170.             s.add(c);
  171.  
  172.           c = new Cube(this);
  173.           c.rotateFrontCW();
  174.           if (!cubesFoundSoFar.contains(c))
  175.             s.add(c);
  176.  
  177.           c = new Cube(this);
  178.           c.rotateRightCW();
  179.           if (!cubesFoundSoFar.contains(c))
  180.             s.add(c);
  181.  
  182.           c = new Cube(this);
  183.           c.rotateRight2();
  184.           if (!cubesFoundSoFar.contains(c))
  185.             s.add(c);
  186.  
  187.           c = new Cube(this);
  188.           c.rotateRightCCW();
  189.           if (!cubesFoundSoFar.contains(c))
  190.             s.add(c);
  191.  
  192.           c = new Cube(this);
  193.           c.rotateUpCW();
  194.           if (!cubesFoundSoFar.contains(c))
  195.             s.add(c);
  196.  
  197.           c = new Cube(this);
  198.           c.rotateUp2();
  199.           if (!cubesFoundSoFar.contains(c))
  200.             s.add(c);
  201.  
  202.           c = new Cube(this);
  203.           c.rotateUpCCW();
  204.           if (!cubesFoundSoFar.contains(c))
  205.             s.add(c);
  206.  
  207.           return s;
  208.         }
  209.    
  210.     /*
  211.       print_sides() is to format the 2d array
  212.       in such a way that it looks like flat cube
  213.            [][]
  214.            [][]
  215.        [][][][][][][][]
  216.        [][][][][][][][]
  217.            [][]
  218.            [][]
  219.      */
  220.        
  221.     private void print_sides(int cube[][], int first_side, int side_count) {
  222.         char ColorNames [] = {'W','O','G','R','B','Y'};
  223.         for(int line = 0; line < 2; line++) {
  224.             for(int side = first_side; side < side_count + first_side; side++) {
  225.                 System.out.print(" ");
  226.                 if(side_count == 1 || side_count == 6) {
  227.                         System.out.print("   ");
  228.                     }
  229.                 for(int column = 0; column < 2; column++) {
  230.                     int sticker = (2 * line) + column;
  231.                     System.out.print(ColorNames[cube[side][sticker]]);
  232.                 }
  233.             }
  234.         System.out.println();
  235.         }
  236.     }
  237.    
  238.    
  239.     @Override
  240.     public String toString() {
  241.         final String NL = System.lineSeparator();
  242.         String acc = NL;
  243.         acc += "   " + cube[TOP][0] + cube[TOP][1] + NL;
  244.         acc += "   " + cube[TOP][2] + cube[TOP][3] + NL;
  245.  
  246.         acc += "" + cube[LEFT][0] + cube[LEFT][1] + " " + cube[FRONT][0] + cube[FRONT][1] + " " + cube[RIGHT][0] + cube[RIGHT][1] + " " + cube[BACK][0] + cube[BACK][1] + NL;
  247.         acc += "" + cube[LEFT][2] + cube[LEFT][3] + " " + cube[FRONT][2] + cube[FRONT][3] + " " + cube[RIGHT][2] + cube[RIGHT][3] + " " + cube[BACK][2] + cube[BACK][3] + NL;
  248.  
  249.         acc += "   " + cube[BOTTOM][0] + cube[BOTTOM][1] + NL;
  250.         acc += "   " + cube[BOTTOM][2] + cube[BOTTOM][3] + NL;
  251.         return acc;
  252.     }
  253.    
  254.     private int[][] print_cube(int [][] cube) {
  255.         print_sides(cube, 0, 1);
  256.         print_sides(cube, 1, 4);
  257.         print_sides(cube, 5, 1);
  258.         return cube;
  259.     }
  260.  
  261.     private void rotateFrontCCW() {
  262.         rotateFaceCCW(FRONT);
  263.  
  264.         int temp1 = cube[TOP][2];
  265.         int temp2 = cube[TOP][3];
  266.  
  267.         cube[TOP][2] = cube[RIGHT][0];
  268.         cube[TOP][3] = cube[RIGHT][2];
  269.  
  270.         cube[RIGHT][0] = cube[BOTTOM][1];
  271.         cube[RIGHT][2] = cube[BOTTOM][0];
  272.  
  273.         cube[BOTTOM][0] = cube[LEFT][1];
  274.         cube[BOTTOM][1] = cube[LEFT][3];
  275.  
  276.         cube[LEFT][1] = temp2;
  277.         cube[LEFT][3] = temp1;
  278.     }
  279.  
  280.     private void rotateFrontCW() {
  281.         rotateFaceCW(FRONT);
  282.  
  283.         int temp1 = cube[TOP][2];
  284.         int temp2 = cube[TOP][3];
  285.  
  286.         cube[TOP][2] = cube[LEFT][3];
  287.         cube[TOP][3] = cube[LEFT][1];
  288.  
  289.         cube[LEFT][1] = cube[BOTTOM][0];
  290.         cube[LEFT][3] = cube[BOTTOM][1];
  291.  
  292.         cube[BOTTOM][0] = cube[RIGHT][2];
  293.         cube[BOTTOM][1] = cube[RIGHT][0];
  294.  
  295.         cube[RIGHT][0] = temp1;
  296.         cube[RIGHT][2] = temp2;
  297.  
  298.     }
  299.    
  300.     private void rotateFront2(){
  301.         rotateFace2(FRONT);
  302.        
  303.         int temp1 = cube[TOP][2];
  304.         int temp2 = cube[TOP][3];
  305.         int temp3 = cube[LEFT][1];
  306.         int temp4 = cube[LEFT][3];
  307.        
  308.         cube[TOP][2] = cube[BOTTOM][1];
  309.         cube[TOP][3] = cube[BOTTOM][0];
  310.        
  311.         cube[LEFT][1] = cube[RIGHT][2];
  312.         cube[LEFT][3] = cube[RIGHT][0];
  313.        
  314.         cube[RIGHT][2] = temp3;
  315.         cube[RIGHT][0] = temp4;
  316.        
  317.         cube[BOTTOM][1] = temp1;
  318.         cube[BOTTOM][0] = temp2;
  319.  
  320.     }
  321.    
  322.    
  323.  
  324.     private void rotateRightCCW() {
  325.         rotateFaceCCW(RIGHT);
  326.  
  327.         int temp1 = cube[TOP][1];
  328.         int temp2 = cube[TOP][3];
  329.  
  330.         cube[TOP][3] = cube[BACK][0];
  331.         cube[TOP][1] = cube[BACK][2];
  332.  
  333.         cube[BACK][0] = cube[BOTTOM][3];
  334.         cube[BACK][2] = cube[BOTTOM][1];
  335.  
  336.         cube[BOTTOM][1] = cube[FRONT][1];
  337.         cube[BOTTOM][3] = cube[FRONT][3];
  338.        
  339.         cube[FRONT][1] = temp1;
  340.         cube[FRONT][3] = temp2;
  341.     }
  342.  
  343.     private void rotateRightCW() {
  344.         rotateFaceCW(RIGHT);
  345.  
  346.         int temp1 = cube[TOP][1];
  347.         int temp2 = cube[TOP][3];
  348.  
  349.         cube[TOP][1] = cube[FRONT][1];
  350.         cube[TOP][3] = cube[FRONT][3];
  351.  
  352.         cube[FRONT][1] = cube[BOTTOM][1];
  353.         cube[FRONT][3] = cube[BOTTOM][3];
  354.  
  355.         cube[BOTTOM][1] = cube[BACK][2];
  356.         cube[BOTTOM][3] = cube[BACK][0];
  357.  
  358.         cube[BACK][2] = temp1;
  359.         cube[BACK][0] = temp2;
  360.  
  361.     }
  362.    
  363.     private void rotateRight2(){
  364.         rotateFace2(RIGHT);
  365.        
  366.         int temp1 = cube[TOP][1];
  367.         int temp2 = cube[TOP][3];
  368.         int temp3 = cube[FRONT][1];
  369.         int temp4 = cube[FRONT][3];
  370.        
  371.         cube[TOP][1] = cube[BOTTOM][1];
  372.         cube[TOP][3] = cube[BOTTOM][3];
  373.        
  374.         cube[FRONT][1] = cube[BACK][2];
  375.         cube[FRONT][3] = cube[BACK][0];
  376.        
  377.         cube[BACK][2] = temp3;
  378.         cube[BACK][0] = temp4;
  379.        
  380.         cube[5][3] = temp1;
  381.         cube[5][1] = temp2;
  382.        
  383.     }
  384.    
  385.     private void rotateUpCW(){
  386.         rotateFaceCW(TOP);
  387.        
  388.         int temp1 = cube[FRONT][0];
  389.         int temp2 = cube[FRONT][1];
  390.        
  391.         cube[FRONT][0] = cube[RIGHT][0];
  392.         cube[FRONT][1] = cube[RIGHT][1];
  393.        
  394.         cube[RIGHT][0] = cube[BACK][0];
  395.         cube[RIGHT][1] = cube[BACK][1];
  396.        
  397.         cube[BACK][0] = cube[LEFT][0];
  398.         cube[BACK][1] = cube[LEFT][1];
  399.        
  400.         cube[LEFT][0] = temp1;
  401.         cube[LEFT][1] = temp2;
  402.  
  403.     }
  404.    
  405.     private void rotateUpCCW(){
  406.         rotateFaceCCW(TOP);
  407.        
  408.         int temp1 = cube[FRONT][0];
  409.         int temp2 = cube[FRONT][1];
  410.        
  411.         cube[FRONT][0] = cube[LEFT][0];
  412.         cube[FRONT][1] = cube[LEFT][1];
  413.        
  414.         cube[LEFT][0] = cube[BACK][0];
  415.         cube[LEFT][1] = cube[BACK][1];
  416.        
  417.         cube[BACK][0] = cube[RIGHT][0];
  418.         cube[BACK][1] = cube[RIGHT][1];
  419.        
  420.         cube[RIGHT][0] = temp1;
  421.         cube[RIGHT][1] = temp2;
  422.  
  423.     }
  424.    
  425.     private void rotateUp2(){
  426.         rotateFace2(TOP);
  427.        
  428.         int temp1 = cube[FRONT][0];
  429.         int temp2 = cube[FRONT][1];
  430.         int temp3 = cube[LEFT][0];
  431.         int temp4 = cube[LEFT][1];
  432.        
  433.         cube[FRONT][0] = cube[BACK][0];
  434.         cube[FRONT][1] = cube[BACK][1];
  435.        
  436.         cube[LEFT][0] = cube[RIGHT][0];
  437.         cube[LEFT][1] = cube[RIGHT][1];
  438.        
  439.         cube[RIGHT][0] = temp3;
  440.         cube[RIGHT][1] = temp4;
  441.        
  442.         cube[BACK][0] = temp1;
  443.         cube[BACK][1] = temp2;
  444.     }
  445.    
  446.     private void rotateFace2(int faceNum) {
  447.         int[] face = cube[faceNum];
  448.         int temp1 = face[1];
  449.         int temp2 = face[3];  
  450.           face[1] = face[2];
  451.           face[3] = face[0];
  452.           face[2] = temp2;
  453.           face[0] = temp1;
  454.     }
  455.    
  456.     private void rotateFaceCW(int faceNum) {
  457.         int[] face = cube[faceNum];
  458.         int temp = face[0];
  459.         face[0] = face[2];
  460.         face[2] = face[3];
  461.         face[3] = face[1];
  462.         face[1] = temp;
  463.     }
  464.  
  465.     private void rotateFaceCCW(int faceNum) {
  466.         int[] face = cube[faceNum];
  467.         int temp = face[0];
  468.         face[0] = face[1];
  469.         face[1] = face[3];
  470.         face[3] = face[2];
  471.         face[2] = temp;
  472.     }
  473.  
  474. }
Advertisement
Add Comment
Please, Sign In to add comment