Skip to content

Instantly share code, notes, and snippets.

@NargiT
Created February 6, 2018 23:22
Show Gist options
  • Save NargiT/fd6126afbb9d0cfbba1cbc3e65756c27 to your computer and use it in GitHub Desktop.
Save NargiT/fd6126afbb9d0cfbba1cbc3e65756c27 to your computer and use it in GitHub Desktop.
Skillvalue - Cells
package fr.nargit.cells;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Main {
public static void main(String[] args) {
BufferedReader in;
try {
int steps = 0;
in = new BufferedReader(new FileReader(new File(args[0])));
steps = Integer.valueOf(in.readLine());
String[] line = in.readLine().split(",");
// String[] line = new String[]{"0", "1", "1", "1"};
// steps = 4;
int[] result = null;
int[] initialState = new int[line.length];
for (int i = 0; i < line.length; i++) {
initialState[i] = Integer.valueOf(line[i]);
}
/* YOUR CODE HERE */
final int size = initialState.length;
for (int k = 0; k < steps; k++) {
int[] states = new int[size];
for (int i = 0; i < size; i++) {
int left = left(i, size);
int right = right(i, size);
if (initialState[left] == 1 && initialState[right] == 1) {
states[i] = 0;
} else if (initialState[left] == 0 && initialState[right] == 0) {
states[i] = 0;
} else {
states[i] = 1;
}
}
initialState = states.clone();
}
result = initialState;
for (int j = 0; j < result.length; j++) {
System.out.print(result[j]);
if (j < result.length - 1) {
System.out.print(",");
}
}
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
static int left(int position, int size) {
if (position == 0) {
return size - 1;
}
return position - 1;
}
static int right(int position, int size) {
if (position == size - 1) {
return 0;
}
return position + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment