Skip to content

Instantly share code, notes, and snippets.

@crevier
Last active March 15, 2018 15:51
Show Gist options
  • Save crevier/ade6012c842f8cf611c5f448003146ee to your computer and use it in GitHub Desktop.
Save crevier/ade6012c842f8cf611c5f448003146ee to your computer and use it in GitHub Desktop.
Bowling kata - constrains : fake outside in and golf.
import org.junit.Test;
import static java.util.Arrays.stream;
import static java.util.stream.IntStream.range;
import static org.junit.Assert.assertEquals;
public class Bowling {
private int score(int[][]f) {
return range(0,9).boxed().mapToInt(i->stream(f[i]).sum()+((f[i][0]+f[i][1]==10)?f[i+1][0]:0)+((f[i][0]==10)?(f[i+1][0]!=10?f[i+1][1]:(i!=8?f[i+2][0]:f[i+1][2])):0)).sum()+stream(f[9]).sum();
}
@Test
public void scoreTests() {
assertEquals(115, score(new int[][]{{5, 2}, {2, 6}, {4, 4}, {7, 3}, {2, 5}, {4, 6}, {4, 2}, {5, 5}, {10, 0}, {8, 2, 3}}));
assertEquals(60, score(new int[][]{{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {10, 0}, {10, 10, 10}}));
assertEquals(40, score(new int[][]{{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {10, 0}, {5, 5, 10}}));
assertEquals(16, score(new int[][]{{10, 0}, {1, 2}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}));
assertEquals(300, score(new int[][]{{10, 0}, {10, 0}, {10, 0}, {10, 0}, {10, 0}, {10, 0}, {10, 0}, {10, 0}, {10, 0}, {10, 10, 10}}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment