Compare commits

..

No commits in common. "32cab0c401d8e67533db32f537d2219231a9902c" and "a0b4c83c445e1b95f5ff180059c04765024bbaef" have entirely different histories.

View File

@ -6,72 +6,44 @@ const joltageBanks = new Array();
* *
* @param {String} bank * @param {String} bank
*/ */
function getHighestValues(bank) { function getHighestJoltages(bank) {
// Turn the string into an array of battery joltage values and their associated index // Turn the string into an array of battery joltage values and their associated index
const values = new Array(); const joltages = new Array();
for (let i = 0; i < bank.length; i++) { for (let i = 0; i < bank.length; i++) {
values.push({ joltages.push({
"value": bank[i], "value": bank[i],
"index": i "index": i
}); });
} }
// Sort them highest to lowest // Sort them highest to lowest
values.sort((a, b) => { joltages.sort((a, b) => {
// Sort by values primarily
if (a.value < b.value) return 1; if (a.value < b.value) return 1;
// If the values are the same, sort by index smallest to highest
else if (a.value === b.value) {
if (a.index > b.index) return 1;
}
else return -1; else return -1;
}); });
// Edge case if the highest number is the final number const finalJoltages = [
if (values[0].index == bank.length - 1) { joltages[0]
const tmp0 = values[0];
let tmp1 = values[1];
let nextLowestFound = false;
let k = 1;
while(nextLowestFound == false) {
if (values[k].value < values[0].value) {
tmp1 = values[k];
nextLowestFound = true;
} else {
k++;
}
}
values[0] = tmp1;
values[1] = tmp0;
}
const finalValues = [
values[0]
]; ];
let foundValid = false; let foundValid = false;
let j = 1; let j = 1;
while (foundValid == false) { while (foundValid == false) {
if (values[j].index > values[0].index) { if (joltages[j].index > joltages[0].index) {
finalValues.push(values[j]); finalJoltages.push(joltages[j]);
foundValid = true; foundValid = true;
} else { } else {
j++; j++;
} }
} }
return finalValues; return finalJoltages;
} }
let counter = 1; let counter = 0;
inputs.forEach(bank => { inputs.forEach(bank => {
console.log("Bank: ", counter); console.log("Bank: ", counter);
const highestJoltages = getHighestValues(bank); const highestJoltages = getHighestJoltages(bank);
if (highestJoltages[0].index > highestJoltages[1].index) {
console.error(`Indexes must be in order!`);
}
joltageBanks.push("".concat(highestJoltages[0].value, highestJoltages[1].value));
counter++; counter++;
}); });