Facebook
From Stained Meerkat, 5 Years ago, written in Java.
This paste is a reply to Untitled from Colorant Macaque - go back
Embed
Viewing differences between Untitled and Re: Untitled
public static void main(String[] args) {
    System.out.println(cellCompete2(new int[]{1, 1, 1, 0, 1, 1, 1, 1}, 2));
    System.out.println(cellCompete(new int[]{1, 1, 1, 0, 1, 1, 1, 1}, 2));
}
public static List cellCompete(int[] states, int days) {
    int edge = 0;
    int counter = 1;
    int[] temp = new int[states.length];
    while (counter <= days) {
        for (int i = 0; i <= states.length - 1; i++) {
            if (i == 0) {
                temp[i] = states[i + 1] == edge ? 0 :1;
: 1;
            } else if (i == states.length - 1) {
                temp[i] = states[i - 1] == edge ? 0 : 1;
            } else {
                temp[i] = states[i - 1] == states[i + 1] ? 0 : 1;
            }
        }
        counter++;
        System.arraycopy(temp, 0, states,0, states, 0, states.length);
    }
    List list = Arrays.stream(states).boxed().collect(Collectors.toList());
    return list;
}}

public static List cellCompete2(int[] states, int days) {
    int temp = 0;
    int[] newStates = new int[states.length];
    while (days > 0) {
        for (int i = 1; i <= states.length - 1; i++) {
            if (temp == i) {
                newStates[i-1] = states[i - 1] == 0 ? 1 : 0;
            }
            temp = states[i];
        }
        days--;
        System.arraycopy(newStates, 0, states, 0, states.length);
    }
    List list = Arrays.stream(states).boxed().collect(Collectors.toList());
    return list;
}