Compare commits
2 Commits
a0b4c83c44
...
32cab0c401
| Author | SHA1 | Date | |
|---|---|---|---|
| 32cab0c401 | |||
| 0483ec9d32 |
50
src/03.js
50
src/03.js
@ -6,44 +6,72 @@ 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) {
|
||||||
|
if (a.index > b.index) return 1;
|
||||||
|
}
|
||||||
else return -1;
|
else return -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
const finalJoltages = [
|
// Edge case if the highest number is the final number
|
||||||
joltages[0]
|
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 (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 (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 = 0;
|
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) {
|
||||||
|
console.error(`Indexes must be in order!`);
|
||||||
|
}
|
||||||
|
joltageBanks.push("".concat(highestJoltages[0].value, highestJoltages[1].value));
|
||||||
counter++;
|
counter++;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user