Skip to content

Instantly share code, notes, and snippets.

@anaarezo
Last active June 28, 2024 10:12
Show Gist options
  • Save anaarezo/ccb4f79702047c469eeb1bfa494305d6 to your computer and use it in GitHub Desktop.
Save anaarezo/ccb4f79702047c469eeb1bfa494305d6 to your computer and use it in GitHub Desktop.
Amazon GDC algorithm
var houses = [1, 0, 0, 0, 0, 1, 0, 0];
var days = 1;

function cellCompete(state, days){
  if(state.length > 8 || days < 1){
    return false;
  }

  var currentIndex, previousValue, nextValue;

  for(var i = 0; i < days; i++){
    currentIndex = 0;
    previousValue = 0;
    nextValue = 0;

    //Scrolls the entire array for each index
    while(currentIndex < state.length){
      //Not yet the last array index, so you can continue to add the next value
      if(currentIndex < state.length - 1){
        nextValue = state[currentIndex + 1];
      }  

      //Calculation of adjacent cell value
      if(nextValue == previousValue){
        previousValue = state[currentIndex];
        state[currentIndex] = 0;
      }else{
        previousValue = state[currentIndex];
        state[currentIndex] = 1;
      }

      //Increment of the current internal loop index
      currentIndex++;
    }
  }

  return state;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment