Skip to content

Instantly share code, notes, and snippets.

@evandocarmo
Last active October 6, 2017 21:33
Show Gist options
  • Save evandocarmo/ac2040e709bbf1a49edb36ef4a34de3e to your computer and use it in GitHub Desktop.
Save evandocarmo/ac2040e709bbf1a49edb36ef4a34de3e to your computer and use it in GitHub Desktop.
Equal Stacks
h1 = readLine().split(' ');
stacks['h1']['array'] = h1.map(Number);
h2 = readLine().split(' ');
stacks['h2']['array'] = h2.map(Number);
h3 = readLine().split(' ');
stacks['h3']['array'] = h3.map(Number);
function pass() {
let max = 0;
let maxName = '';
//calculate all heights
for (let property in stacks) {
let height;
if (stacks[property].array.length !== 0) {
height = stacks[property].array.reduce((a, b) => a + b);
} else {
height = 0;
}
stacks[property].height = height;
if (stacks[property].height > max) {
max = stacks[property].height;
maxName = property;
}
}
//if heights are equal, we are done. return.
if (stacks.h1.height === stacks.h2.height && stacks.h1.height === stacks.h3.height) {
console.log(stacks.h1.height);
return;
}
//remove top block
stacks[maxName].array.shift();
//let's check here if it makes the heights equal to avoid passing again
if (stacks[maxName].array.length !== 0) {
stacks[maxName].height = stacks[maxName].array.reduce((a, b) => a + b);
if (stacks.h1.height === stacks.h2.height && stacks.h1.height === stacks.h3.height) {
console.log(stacks.h1.height);
return;
}
} else {
console.log(0);
return;
}
//If it doesn't, let's do it again
pass();
}
pass();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment