Skip to content

Instantly share code, notes, and snippets.

@runandrerun
Created November 13, 2018 17:11
Show Gist options
  • Save runandrerun/a0d737af2cc8eb9a1de8061f6e39b093 to your computer and use it in GitHub Desktop.
Save runandrerun/a0d737af2cc8eb9a1de8061f6e39b093 to your computer and use it in GitHub Desktop.
Combination Lock Problem Set
combinationLock = (initialState, code) => {
let totalTurns = 0;
initialState.forEach((i, index) => {
let turns = Math.abs(i - code[index]);
if (turns <= 5) {
return (totalTurns += turns);
} else {
const newTurns = 10 - turns;
return (totalTurns += newTurns);
}
});
return totalTurns;
};
combinationLock([0, 0, 0], [3, 1, 8])
combinationLock([2, 3, 4, 5], [5, 4, 3, 2])
combinationLock([1, 9, 1, 9], [0, 0, 0, 0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment