Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- class LoopUnrollBenchmark {
- public static void main(String[] args) {
- //int[] input = {0, 1, 0, 1, 1, 0, 0, 1};
- int[] input = {1,0,0,1,0,0,1,0};
- int N = 1000000000;
- int benchmarkIters = 10;
- long[] u0 = new long[benchmarkIters];
- long[] u10 = new long[benchmarkIters];
- long[] u100 = new long[benchmarkIters];
- long start, fin;
- for (int i = 0; i < benchmarkIters; i++) {
- start = System.nanoTime();
- noUnroll(input, N);
- fin = System.nanoTime() - start;
- u0[i] = fin;
- start = System.nanoTime();
- unroll10(input, N);
- fin = System.nanoTime() - start;
- u10[i] = fin;
- start = System.nanoTime();
- unroll100(input, N);
- fin = System.nanoTime() - start;
- u100[i] = fin;
- System.out.println("Finished iteration " + i + " out of " + benchmarkIters);
- }
- System.out.println("Averages over " + benchmarkIters + " iterations");
- System.out.println("No Unroll: " + Arrays.stream(u0).sum()/benchmarkIters/1000 + " ms");
- System.out.println("10 Unroll: " + Arrays.stream(u10).sum()/benchmarkIters/1000 + " ms");
- System.out.println("100 Unroll: " + Arrays.stream(u100).sum()/benchmarkIters/1000 + " ms");
- //Arrays.stream(cells).forEach(System.out::print);
- }
- public static int[] noUnroll(int[] cells, int N) {
- int[] newCells = new int[8];
- newCells[0] = 0;
- newCells[7] = 0;
- while (N > 0) {
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- N--;
- }
- return cells;
- }
- public static int[] unroll10(int[] cells, int N) {
- //if (N == 0) return cells;
- int[] newCells = new int[8];
- newCells[0] = 0;
- newCells[7] = 0;
- while (N > 10) {
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // ----------------------------------
- //copy another 5
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // ----------------------------------
- N-=10;
- }
- while (N > 0) {
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- // ----------------------------------
- N--;
- }
- return cells;
- // return prisonAfterNDays(newCells, N-1);
- }
- public static int[] unroll100(int[] cells, int N) {
- //if (N == 0) return cells;
- int[] newCells = new int[8];
- newCells[0] = 0;
- newCells[7] = 0;
- while (N > 100){
- // 0
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // 10
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // 20
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // 30
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // 40
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // 50
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // 60
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- //70
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // 80
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- // 90
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- //100
- N-=100;
- }
- while (N > 10) {
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- N-=10;
- }
- while (N > 0) {
- // ----------------------------------
- for (int i = 1; i < 7; i++) {
- if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
- newCells[i] = 1;
- } else {
- newCells[i] = 0;
- }
- }
- for (int i = 1; i < 7; i++) cells[i] = newCells[i];
- cells[0] = 0;
- cells[7] = 0;
- // ----------------------------------
- N--;
- }
- return cells;
- }
- }
Add Comment
Please, Sign In to add comment