Skip to content

Instantly share code, notes, and snippets.

@hueitan
Last active August 29, 2015 14:19
Show Gist options
  • Save hueitan/be354bf564087036354e to your computer and use it in GitHub Desktop.
Save hueitan/be354bf564087036354e to your computer and use it in GitHub Desktop.
function blackOrWhite(s) { // true: black, false: white
  var vertical = s[0].charCodeAt(0) - 'a'.charCodeAt(0) + 1;
  var horizon = s[1];
  if (vertical % 2 == 1) { // vertical odd
    if (horizon % 2 == 0) { // horizon even
      return false;
    }
    return true;
  } else { // vertical even
    if (horizon % 2 == 0) { // horizon even
      return true;
    }
    return false;
  }
  // odd + odd   => even (true)
  // odd + even  => odd  (false)
  // even + even => even (true)
  // even + odd  => odd  (false)
  // therefore we can minify to
  // return (+vertical + +horizon) % 2;
}

function WhoseTurn(p) {
  var token = p.split(';');
  var whose = 1;

  if (blackOrWhite(token[0])) whose *= -1;
  if (blackOrWhite(token[1])) whose *= -1;
  if (blackOrWhite(token[2])) whose *= -1;
  if (blackOrWhite(token[3])) whose *= -1;

  return whose == 1 ? 0 : 1;
}

// full algorithm here

my best minify code (80 chars) - more beautiful

for (k in b=arguments[a = 0].split(';'))
   a += b[k][0].charCodeAt(0) % 2 + b[k][1] % 2

return a % 2

(74 chars)

b=arguments[k=a=0]

for (;b[k];k+=3)
   a += b[k].charCodeAt(0) % 2 + b[k+1] % 2

return a % 2

somehow a better readable solution

e = 1
for (i in p = arguments[0])   
  e+= p.charCodeAt(i)    
return e%2

generic way in crazy one line!

return parseInt(arguments[0].replace(/;/g, 0), 19) % 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment