Skip to content

Instantly share code, notes, and snippets.

@simonharrer
Created October 28, 2011 12:33
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 simonharrer/1322165 to your computer and use it in GitHub Desktop.
Save simonharrer/1322165 to your computer and use it in GitHub Desktop.
Calculation of the possibilities having three processes with 3, 3 and 2 steps being interleaved.
import groovy.util.PermutationGenerator;
import java.util.LinkedList;
import java.util.List;
import com.google.common.collect.Ordering;
public class Choice {
public static void main(String[] args) {
List<Integer> values = new LinkedList<>();
for (int i = 1; i <= 8; i++) {
values.add(i);
}
PermutationGenerator<Integer> gen = new PermutationGenerator<>(values);
int sum = 0;
System.out.println("Total " + gen.getTotal());
while (gen.hasNext()) {
if (isValid(gen.next())) {
sum++;
}
}
System.out.println(sum);
}
private static boolean isValid(List<Integer> list) {
List<Integer> p1 = new LinkedList<>();
List<Integer> p2 = new LinkedList<>();
List<Integer> p3 = new LinkedList<>();
for (Integer i : list) {
if (i < 4) {
p1.add(i);
} else if (i < 6) {
p2.add(i);
} else {
p3.add(i);
}
}
Ordering<Integer> ordering = Ordering.natural();
return ordering.isOrdered(p1) && ordering.isOrdered(p2) && ordering.isOrdered(p3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment