Skip to content

Instantly share code, notes, and snippets.

@freddiefujiwara
Created December 16, 2011 00:06
Show Gist options
  • Select an option

  • Save freddiefujiwara/1483616 to your computer and use it in GitHub Desktop.

Select an option

Save freddiefujiwara/1483616 to your computer and use it in GitHub Desktop.
var fields = "W........WW.";
fields += ".WWW.....WWW";
fields += "....WW...WW.";
fields += ".........WW.";
fields += ".........W..";
fields += "..W......W..";
fields += ".W.W.....WW.";
fields += "W.W.W.....W.";
fields += ".W.W......W.";
fields += "..W.......W.";
var width = 12;
fields = fields.split(new RegExp(""));
var walk = function(i){
fields[i] = ".";
$.each([i-width-1,i-width,i-width+1,i-1,i+1,i+width-1,i+width,i+width+1],function(n,m){
if("W"===fields[m]){walk(m);};
});
};
var count = 0;
$.each(fields,function(n,m){
if("W"===fields[n]){walk(n);count++;};
});
console.log(count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment