Facebook
From Stained Meerkat, 4 Years ago, written in Java.
This paste is a reply to Untitled from Colorant Macaque - view diff
Embed
Download Paste or View Raw
Hits: 229
  1. public static void main(String[] args) {
  2.     System.out.println(cellCompete2(new int[]{1, 1, 1, 0, 1, 1, 1, 1}, 2));
  3.     System.out.println(cellCompete(new int[]{1, 1, 1, 0, 1, 1, 1, 1}, 2));
  4. }
  5. public static List<Integer> cellCompete(int[] states, int days) {
  6.     int edge = 0;
  7.     int counter = 1;
  8.     int[] temp = new int[states.length];
  9.     while (counter <= days) {
  10.         for (int i = 0; i <= states.length - 1; i++) {
  11.             if (i == 0) {
  12.                 temp[i] = states[i + 1] == edge ? 0 : 1;
  13.             } else if (i == states.length - 1) {
  14.                 temp[i] = states[i - 1] == edge ? 0 : 1;
  15.             } else {
  16.                 temp[i] = states[i - 1] == states[i + 1] ? 0 : 1;
  17.             }
  18.         }
  19.         counter++;
  20.         System.arraycopy(temp, 0, states, 0, states.length);
  21.     }
  22.     List<Integer> list = Arrays.stream(states).boxed().collect(Collectors.toList());
  23.     return list;
  24. }
  25.  
  26. public static List<Integer> cellCompete2(int[] states, int days) {
  27.     int temp = 0;
  28.     int[] newStates = new int[states.length];
  29.     while (days > 0) {
  30.         for (int i = 1; i <= states.length - 1; i++) {
  31.             if (temp == i) {
  32.                 newStates[i-1] = states[i - 1] == 0 ? 1 : 0;
  33.             }
  34.             temp = states[i];
  35.         }
  36.         days--;
  37.         System.arraycopy(newStates, 0, states, 0, states.length);
  38.     }
  39.     List<Integer> list = Arrays.stream(states).boxed().collect(Collectors.toList());
  40.     return list;
  41. }
  42.