Created
October 20, 2015 02:12
-
-
Save KevinTCoughlin/7932ae8fd5d00698c81d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var chars = 'OISZLJT'; | |
| // Generates a string of tetris pieces of n-length | |
| function tetris(n) { | |
| var pieces = chars.split(''); | |
| return _.map(_.times(n, function() { | |
| var piece = _.sample(pieces); | |
| pieces = _.without(pieces, piece); | |
| if (pieces.length <= 0) { | |
| pieces = chars.split(''); | |
| } | |
| return piece; | |
| })).join(''); | |
| } | |
| // Verify that the tetris string is valid | |
| // i.e. no more than 1 unique character per | |
| // seven characters. | |
| function verify(s) { | |
| var valid = true; | |
| var chunks = _.chunk(s, 7); | |
| for (var i = 0; i < chunks.length; i++) { | |
| var chunk = chunks[i]; | |
| var sorted = chunk.sort(); | |
| var curr = sorted[0]; | |
| for (var j = 1; j < sorted.length; j++) { | |
| if (curr === sorted[j]) { | |
| valid = false; | |
| break; | |
| } else { | |
| curr = sorted[j]; | |
| } | |
| } | |
| } | |
| return valid; | |
| } | |
| var result = tetris(50); | |
| console.log(result, verify(result)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment