Cleaned up a little
This commit is contained in:
parent
0483ec9d32
commit
32cab0c401
38
src/03.js
38
src/03.js
@ -6,66 +6,68 @@ const joltageBanks = new Array();
|
||||
*
|
||||
* @param {String} bank
|
||||
*/
|
||||
function getHighestJoltages(bank) {
|
||||
function getHighestValues(bank) {
|
||||
// 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++) {
|
||||
joltages.push({
|
||||
values.push({
|
||||
"value": bank[i],
|
||||
"index": i
|
||||
});
|
||||
}
|
||||
|
||||
// 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 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;
|
||||
});
|
||||
|
||||
if (joltages[0].index == bank.length - 1) {
|
||||
const tmp0 = joltages[0];
|
||||
let tmp1;
|
||||
// Edge case if the highest number is the final number
|
||||
if (values[0].index == bank.length - 1) {
|
||||
const tmp0 = values[0];
|
||||
let tmp1 = values[1];
|
||||
let nextLowestFound = false;
|
||||
let k = 1;
|
||||
while(nextLowestFound == false) {
|
||||
if (joltages[k].value < joltages[0].value) {
|
||||
tmp1 = joltages[k];
|
||||
if (values[k].value < values[0].value) {
|
||||
tmp1 = values[k];
|
||||
nextLowestFound = true;
|
||||
} else {
|
||||
k++;
|
||||
}
|
||||
}
|
||||
joltages[0] = tmp1;
|
||||
joltages[1] = tmp0;
|
||||
values[0] = tmp1;
|
||||
values[1] = tmp0;
|
||||
}
|
||||
|
||||
const finalJoltages = [
|
||||
joltages[0]
|
||||
const finalValues = [
|
||||
values[0]
|
||||
];
|
||||
|
||||
let foundValid = false;
|
||||
let j = 1;
|
||||
while (foundValid == false) {
|
||||
if (joltages[j].index > joltages[0].index) {
|
||||
finalJoltages.push(joltages[j]);
|
||||
if (values[j].index > values[0].index) {
|
||||
finalValues.push(values[j]);
|
||||
foundValid = true;
|
||||
} else {
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
return finalJoltages;
|
||||
return finalValues;
|
||||
}
|
||||
|
||||
let counter = 1;
|
||||
|
||||
|
||||
inputs.forEach(bank => {
|
||||
console.log("Bank: ", counter);
|
||||
const highestJoltages = getHighestJoltages(bank);
|
||||
const highestJoltages = getHighestValues(bank);
|
||||
if (highestJoltages[0].index > highestJoltages[1].index) {
|
||||
console.error(`Indexes must be in order!`);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user