Advertisement
lackofcheese

Text3x3.java

Sep 10th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4.  
  5. import org.junit.Assert;
  6. import org.junit.Test;
  7.  
  8. public class Test3x3 {
  9.     /**
  10.      * The required functionality for a solver - must take a test case as a 3x3
  11.      * matrix, and return the total as an integer.
  12.      *
  13.      * @author lackofcheese
  14.      */
  15.     public interface Solver {
  16.         public int solve(int[][] m);
  17.     }
  18.  
  19.     /**
  20.      * Uses the special fast 3x3 algorithm to solve a test case.
  21.      */
  22.     public static Solver special = new Solver() {
  23.         @Override
  24.         public int solve(int[][] m) {
  25.             // Get sorted indices.
  26.             int[] vals = new int[9];
  27.             for (int i = 0; i < 3; i++) {
  28.                 for (int j = 0; j < 3; j++) {
  29.                     vals[3 * i + j] = m[i][j];
  30.                 }
  31.             }
  32.             Integer[] inds = indirectSort(vals);
  33.             int score = 0;
  34.  
  35.             // Calculate overall total
  36.             for (int i = 0; i < 9; i++) {
  37.                 score += vals[i];
  38.             }
  39.             score -= vals[inds[0]] + vals[inds[1]];
  40.  
  41.             // If the three picked values are in a single row or column...
  42.             if ((inds[0] % 3 == inds[1] % 3 && inds[1] % 3 == inds[2] % 3)
  43.                     || (inds[0] / 3 == inds[1] / 3 && inds[1] / 3 == inds[2] / 3)) {
  44.                 score -= vals[inds[3]];
  45.             } else {
  46.                 score -= vals[inds[2]];
  47.             }
  48.             return score;
  49.         }
  50.     };
  51.  
  52.     /**
  53.      * Uses the general algorithm to solve the test case.
  54.      */
  55.     public static Solver general = new Solver() {
  56.         @Override
  57.         public int solve(int[][] m) {
  58.             return Creamsteak.creamsteak(m);
  59.         }
  60.     };
  61.  
  62.     /**
  63.      * An indirect sorting algorithm. Returns the indices of the elements from
  64.      * the one with the lowest value to the one with the highest.
  65.      *
  66.      * @param data the array of integer values.
  67.      * @return an array of Integer indices into the data array.
  68.      */
  69.     public static Integer[] indirectSort(final int[] data) {
  70.         Integer[] inds = new Integer[data.length];
  71.         for (int i = 0; i < inds.length; i++) {
  72.             inds[i] = i;
  73.         }
  74.         Arrays.sort(inds, new Comparator<Integer>() {
  75.             @Override
  76.             public int compare(final Integer o1, final Integer o2) {
  77.                 return data[o1] - data[o2];
  78.             }
  79.         });
  80.         return inds;
  81.     }
  82.  
  83.     /**
  84.      * Checks all 9^9 possible configurations.
  85.      */
  86.     @Test
  87.     public void testAll() {
  88.         checkSameResults(0);
  89.     }
  90.  
  91.     /**
  92.      * Checks one million of the 9^9 possible configurations.
  93.      */
  94.     @Test
  95.     public void test1M() {
  96.         checkSameResults(1000000);
  97.     }
  98.  
  99.     /**
  100.      * Checks the general algorithm against the 3x3 special-case algorithm to
  101.      * ensure they get the same results.
  102.      *
  103.      * @param numTests
  104.      *            The number of cases to take out of the 9^9 possibilities.
  105.      */
  106.     public static void checkSameResults(int numTests) {
  107.         long startTime = System.currentTimeMillis();
  108.         checkSolvers(numTests, general, special);
  109.         System.out.println(String.format("Time taken: %.2fs",
  110.                 (System.currentTimeMillis() - startTime) / 1000.0));
  111.     }
  112.    
  113.     @Test
  114.     /**
  115.      * Checks the total value for a single solver.
  116.      */
  117.     public void checkTotal() {
  118.         long startTime = System.currentTimeMillis();
  119.         BigInteger total = checkSolvers(1000000, general, null);
  120.         // Assert.assertEquals("Wrong total!", total, new BigInteger("2558136"));
  121.         System.out.println(String.format("Time taken: %.2fs",
  122.                 (System.currentTimeMillis() - startTime) / 1000.0));   
  123.     }
  124.  
  125.     /**
  126.      * Checks to make sure that two solvers get the same result for the given
  127.      * number of test cases.
  128.      *
  129.      * @param numTests
  130.      *            the number of test cases to run.
  131.      * @param solver1
  132.      *            the first solver.
  133.      * @param solver2
  134.      *            the second solver.
  135.      * @return the overall total of the results for all of the test cases.
  136.      */
  137.     public static BigInteger checkSolvers(int numTests, Solver solver1,
  138.             Solver solver2) {
  139.         // Array to hold the values in.
  140.         int[][] m = new int[3][3];
  141.  
  142.         // Initialise our total and problem number.
  143.         BigInteger prob = BigInteger.ZERO;
  144.         BigInteger total = BigInteger.ZERO;
  145.  
  146.         // Loop through each of the 9^9 possible combinations.
  147.         outerloop:
  148.         for (m[0][0] = 1; m[0][0] <= 9; m[0][0]++)
  149.         for (m[0][1] = 1; m[0][1] <= 9; m[0][1]++)
  150.         for (m[0][2] = 1; m[0][2] <= 9; m[0][2]++)
  151.         for (m[1][0] = 1; m[1][0] <= 9; m[1][0]++)
  152.         for (m[1][1] = 1; m[1][1] <= 9; m[1][1]++)
  153.         for (m[1][2] = 1; m[1][2] <= 9; m[1][2]++)
  154.         for (m[2][0] = 1; m[2][0] <= 9; m[2][0]++)
  155.         for (m[2][1] = 1; m[2][1] <= 9; m[2][1]++)
  156.         for (m[2][2] = 1; m[2][2] <= 9; m[2][2]++) {
  157.             int score1 = solver1.solve(m);
  158.             if (solver2 != null) {
  159.                 int score2 = solver2.solve(m);
  160.                 Assert.assertEquals(
  161.                         "Different totals for matrix "
  162.                                 + Arrays.deepToString(m),
  163.                                 score1, score2);
  164.             }
  165.             total = total.add(new BigInteger(
  166.                     Integer.toString(score1)));
  167.             prob = prob.add(BigInteger.ONE);
  168.  
  169.             if (prob.equals(new BigInteger(
  170.                     Integer.toString(numTests)))) {
  171.                 break outerloop;
  172.             }
  173.         }
  174.         System.out.println("BigBoss = " + total.toString());
  175.         System.out.println("BigCount = " + prob.toString());
  176.         return total;
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement