Skip to content

Instantly share code, notes, and snippets.

@joriki
Created July 12, 2015 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joriki/5ae058d5d3e9458b9bbd to your computer and use it in GitHub Desktop.
Save joriki/5ae058d5d3e9458b9bbd to your computer and use it in GitHub Desktop.
Try to solve a game of consolidating numbers; see http://math.stackexchange.com/questions/1341929
import java.util.Arrays;
public class Question1341929 {
public static void main (String [] args) {
for (int n = 2;;n++) {
int nslots = 2 * n - 1;
int [] p = new int [nslots];
int max = 0;
int [] best = null;
for (int bits = (1 << (nslots - 2));bits < 1 << (nslots - 1);bits += 2) {
int hi = nslots - 1;
int lo = 0;
for (int i = nslots - 1;i >= 0;i--)
p [i] = (bits & (1 << i)) != 0 ? hi-- : lo++;
int value = value (p);
if (value > max) {
max = value;
best = p.clone ();
}
}
for (int i = 0;i < nslots;i++)
best [i]++; // add 1 for humans
System.out.println (n + " " + max + " " + Arrays.toString (best));
}
}
static int max = 1000;
static int [] v = new int [max];
static int [] left = new int [max];
static int [] right = new int [max];
static int value (int [] p) {
int n = (p.length + 1) >> 1;
for (int i = 1;i <= n;i++)
v [n + i - 2] = v [n - i] = i;
for (int i = 1;i < p.length;i++) {
left [i] = i - 1;
right [i - 1] = i;
}
left [0] = right [p.length - 1] = -1;
for (int i : p)
play (i);
return v [p [p.length - 1]];
}
static void play (int i) {
play (i,left,right);
play (i,right,left);
}
static void play (int i,int [] n1,int [] n2) {
if (n1 [i] != -1) {
v [n1 [i]] += v [i];
n2 [n1 [i]] = n2 [i];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment