Cleaned up a little

This commit is contained in:
Skylar Grant 2025-12-05 16:01:03 -05:00
parent 0483ec9d32
commit 32cab0c401

View File

@ -6,66 +6,68 @@ const joltageBanks = new Array();
* *
* @param {String} bank * @param {String} bank
*/ */
function getHighestJoltages(bank) { function getHighestValues(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 joltages = new Array(); const values = new Array();
for (let i = 0; i < bank.length; i++) { for (let i = 0; i < bank.length; i++) {
joltages.push({ values.push({
"value": bank[i], "value": bank[i],
"index": i "index": i
}); });
} }
// Sort them highest to lowest // Sort them highest to lowest
joltages.sort((a, b) => { values.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) { else if (a.value === b.value) {
if (a.index > b.index) return 1; if (a.index > b.index) return 1;
} }
else return -1; else return -1;
}); });
if (joltages[0].index == bank.length - 1) { // Edge case if the highest number is the final number
const tmp0 = joltages[0]; if (values[0].index == bank.length - 1) {
let tmp1; const tmp0 = values[0];
let tmp1 = values[1];
let nextLowestFound = false; let nextLowestFound = false;
let k = 1; let k = 1;
while(nextLowestFound == false) { while(nextLowestFound == false) {
if (joltages[k].value < joltages[0].value) { if (values[k].value < values[0].value) {
tmp1 = joltages[k]; tmp1 = values[k];
nextLowestFound = true; nextLowestFound = true;
} else { } else {
k++; k++;
} }
} }
joltages[0] = tmp1; values[0] = tmp1;
joltages[1] = tmp0; values[1] = tmp0;
} }
const finalJoltages = [ const finalValues = [
joltages[0] values[0]
]; ];
let foundValid = false; let foundValid = false;
let j = 1; let j = 1;
while (foundValid == false) { while (foundValid == false) {
if (joltages[j].index > joltages[0].index) { if (values[j].index > values[0].index) {
finalJoltages.push(joltages[j]); finalValues.push(values[j]);
foundValid = true; foundValid = true;
} else { } else {
j++; j++;
} }
} }
return finalJoltages; return finalValues;
} }
let counter = 1; let counter = 1;
inputs.forEach(bank => { inputs.forEach(bank => {
console.log("Bank: ", counter); console.log("Bank: ", counter);
const highestJoltages = getHighestJoltages(bank); const highestJoltages = getHighestValues(bank);
if (highestJoltages[0].index > highestJoltages[1].index) { if (highestJoltages[0].index > highestJoltages[1].index) {
console.error(`Indexes must be in order!`); console.error(`Indexes must be in order!`);
} }