I did it in lua bcs I had slightly more complex rules.  Looks like what you have will work, it's just O(n)<br><br>