Skip to content

Instantly share code, notes, and snippets.

@Hiosdra
Created May 29, 2018 23:09
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 Hiosdra/ecbc97c54b1862c71e7f9cc62246c9e5 to your computer and use it in GitHub Desktop.
Save Hiosdra/ecbc97c54b1862c71e7f9cc62246c9e5 to your computer and use it in GitHub Desktop.
package kata.colouredtriangles;
public class Kata {
private static final int R = 0;
private static final int G = 1;
private static final int B = 2;
public static char triangle(final String firstRow) {
StringBuilder row = new StringBuilder(firstRow);
while (row.length() != 1) {
row = triangleOneTime(row);
}
return row.charAt(0);
}
private static StringBuilder triangleOneTime(StringBuilder firstRow) {
StringBuilder row = new StringBuilder();
for(int i = 1; i < firstRow.length(); ++i) {
row.append(getChild(getValue(firstRow.charAt(i -1)), getValue(firstRow.charAt(i))));
}
return row;
}
private static int getValue(char rgb) {
if (rgb == 'R') return R;
if (rgb == 'G') return G;
if (rgb == 'B') return B;
throw new IllegalArgumentException();
}
private static char getChild(int parentBefore, int parentActual) {
switch (parentBefore + parentActual) {
case 0:
case 3: return 'R';
case 2: return 'G';
case 4:
case 1: return 'B';
default: throw new IllegalArgumentException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment