Created
May 29, 2018 23:09
-
-
Save Hiosdra/ecbc97c54b1862c71e7f9cc62246c9e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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