@@ -16,7 +16,7 @@ |
static final int OVER = 1; |
static final int TO = 2; |
|
- static final int[][] MOVES = new int[][]{ |
+ static final int[][] JUMPS = { |
{0, 2, 5}, |
{0, 1, 3}, |
{1, 4, 8}, |
@@ -55,15 +55,24 @@ |
{14, 9, 5} |
}; |
|
+ int numBoards = 0; |
+ int[] solutions = new int[15]; |
+ boolean[] initialBoard = { |
+ true, |
+ true, true, |
+ true, true, true, |
+ true, true, true, true, |
+ true, true, true, true, true |
+ }; |
|
- static void init() { |
- boards = 0; |
- solutions = new int[15]; |
+ int missing; |
+ |
+ |
+ public Solitaire(int missing) { |
+ initialBoard[missing] = false; |
+ this.missing = missing; |
} |
|
- static int boards = 0; |
- static int[] solutions = new int[15]; |
- |
static class Board { |
|
boolean[] board; |
@@ -77,7 +86,6 @@ |
this.from = from; |
this.to = to; |
this.prev = prev; |
- boards++; |
} |
|
|
@@ -95,8 +103,8 @@ |
List<Board> allJumps() { |
List<Board> bs = new LinkedList<>(); |
Board b; |
- for (int[] mv : MOVES) { |
- if ((b = jump(mv[FROM], mv[OVER], mv[TO])) != null) |
+ for (int[] j : JUMPS) { |
+ if ((b = jump(j[FROM], j[OVER], j[TO])) != null) |
bs.add(b); |
} |
return bs; |
@@ -118,34 +126,52 @@ |
return new StringBuffer().append(from) |
.append('\u2192').append(to).toString(); |
} |
- |
} |
|
|
- static void solve(Board b) { |
- List<Board> moves = b.allJumps(); |
- if (moves.isEmpty()) { |
+ void solve(Board b) { |
+ List<Board> boards = b.allJumps(); |
+ if (boards.isEmpty()) { |
+ |
// no more moves |
// if (b.getCount() == 1) |
// System.out.println(allMoves(b)); |
- |
solutions[b.getCount()]++; |
return; |
} |
|
- for (Board board : moves) { |
- solve(board); |
+ numBoards += boards.size(); |
+ |
+ for (Board b1 : boards) { |
+ solve(b1); |
} |
} |
|
|
- static StringBuffer allMoves(Board b) { |
+ void solveAll() { |
+ solve(new Board(initialBoard, 0, 0, null)); |
+ } |
+ |
+ |
+ StringBuffer allMoves(Board b) { |
if (b.prev == null) |
return new StringBuffer(); |
return allMoves(b.prev).append(b).append(", "); |
} |
|
|
+ public void stats() { |
+ System.out.println("Start pos: " + missing); |
+ System.out.println("# of Solutions per remaining pins (0..14): " + Arrays.toString(solutions)); |
+ System.out.println("total # Solutions: " + Arrays.stream(solutions).sum()); |
+ System.out.println("# of 'One remaining pin' Solutions: " + solutions[1]); |
+ System.out.println("% of 'One remaining pin' per total: " + (double) solutions[1] |
+ / Arrays.stream(solutions).sum()); |
+ System.out.println("# Boards: " + numBoards); |
+ System.out.println(); |
+ } |
+ |
+ |
// layout |
// |
// x |
@@ -154,46 +180,24 @@ |
// x x x x |
// x x x x x |
// |
+ // Board position ids (for moves): |
+ // |
// 0 |
- // 1 2 |
- // 3 4 5 |
- // 6 7 8 9 |
+ // 1 2 |
+ // 3 4 5 |
+ // 6 7 8 9 |
// 10 11 12 13 14 |
// |
public static void main(String[] args) { |
CheckPointing cp = new CheckPointing(); |
cp.check("start"); |
- for (int i = 0; i < 15; i++) { |
- m(i); |
+ for (int missing = 0; missing < 15; missing++) { |
+ Solitaire s = new Solitaire(missing); |
+ s.solveAll(); |
+ s.stats(); |
} |
cp.check("done"); |
System.out.println(cp); |
} |
|
- |
- public static void m(int i) { |
- |
- boolean[] start = new boolean[]{ |
- true, |
- true, true, |
- true, true, true, |
- true, true, true, true, |
- true, true, true, true, true |
- }; |
- |
- start[i] = false; |
- |
- init(); |
- Board b = new Board(start, 0, 0, null); |
- solve(b); |
- |
- System.out.println("start pos: " + i); |
- System.out.println("# of Solutions per remaining pins (0..14): " + Arrays.toString(solutions)); |
- System.out.println("total # Solutions: " + Arrays.stream(solutions).sum()); |
- System.out.println("# of 'One remaining pin' Solutions: " + solutions[1]); |
- System.out.println("% of 'One remaining pin' per total: " + (double) solutions[1] / (double) Arrays.stream(solutions).sum()); |
- System.out.println("Boards created on the way: " + boards); |
- System.out.println(); |
- } |
- |
} |
@@ -20,44 +20,47 @@ |
@Path("m/stats/1.0") |
public class V1_0 extends csip.ModelDataService { |
|
- // this is comment. |
- |
- @Override |
- protected void doProcess() throws Exception { |
- |
- List<Double> l = new ArrayList<>(); |
- |
- JSONArray v = parameter().getJSONArray("result"); |
- JSONObject ts = v.getJSONObject(0); |
- JSONArray a = ts.getJSONArray("data"); |
- for (int i = 0; i < a.length(); i++) { |
- JSONArray elem = a.getJSONArray(i); |
- l.add(elem.getDouble(1)); |
- } |
- |
- double[] vals = toDoubleArray(l); |
- System.out.println(l); |
- |
- Stats s = Stats.of(vals); |
- Map<Integer, Double> uncertResult = Quantiles.quartiles().indexes(0, 1, 2, 3, 4).compute(vals); |
- double min = uncertResult.get(0); |
- double q1 = uncertResult.get(1); |
- double med = uncertResult.get(2); |
- double q3 = uncertResult.get(3); |
- double max = uncertResult.get(4); |
- |
- results().put("mean", s.mean(),"Mean"); |
- results().put("count", s.count(),"Count"); |
- results().put("pop_stdev", s.populationStandardDeviation(), "populationStandardDeviation"); |
- results().put("pop_var", s.populationVariance(), "populationVariance"); |
- results().put("min", min,"minimum"); |
- results().put("q1", q1,"1st Quartile"); |
- results().put("median", med,"Median"); |
- results().put("q3", q3,"3nd Quartile"); |
- results().put("max", max,"maximum"); |
+ // this is comment. |
+ @Override |
+ protected void doProcess() throws Exception { |
+ |
+ List<Double> l = new ArrayList<>(); |
+ |
+ JSONArray v = parameter().getJSONArray("result"); |
+ JSONObject ts = v.getJSONObject(0); |
+ JSONArray a = ts.getJSONArray("data"); |
+ for (int i = 0; i < a.length(); i++) { |
+ JSONArray elem = a.getJSONArray(i); |
+ l.add(elem.getDouble(1)); |
} |
- |
- public static double[] toDoubleArray(List<Double> data) { |
- return data.stream().mapToDouble(Double::doubleValue).toArray(); |
- } |
+ |
+ double[] vals = toDoubleArray(l); |
+ System.out.println(l); |
+ |
+ Stats s = Stats.of(vals); |
+ Map<Integer, Double> uncertResult = Quantiles.quartiles().indexes(0, 1, 2, 3, 4).compute(vals); |
+ double min = uncertResult.get(0); |
+ double q1 = uncertResult.get(1); |
+ double med = uncertResult.get(2); |
+ double q3 = uncertResult.get(3); |
+ double max = uncertResult.get(4); |
+ |
+ int i = 0; |
+// for (i = 0; i < 100000; i++) { |
+ results().put("mean" + i , s.mean(), "Mean"); |
+ results().put("count"+ i, s.count(), "Count"); |
+ results().put("pop_stdev+ i", s.populationStandardDeviation(), "populationStandardDeviation"); |
+ results().put("pop_var"+ i, s.populationVariance(), "populationVariance"); |
+ results().put("min"+ i, min, "minimum"); |
+ results().put("q1"+ i, q1, "1st Quartile"); |
+ results().put("median"+ i, med, "Median"); |
+ results().put("q3"+ i, q3, "3nd Quartile"); |
+ results().put("max"+ i, max, "maximum"); |
+// } |
+ } |
+ |
+ |
+ public static double[] toDoubleArray(List<Double> data) { |
+ return data.stream().mapToDouble(Double::doubleValue).toArray(); |
+ } |
} |