Part 1 Done
This commit is contained in:
parent
a0b4c83c44
commit
0483ec9d32
28
src/03.js
28
src/03.js
@ -19,12 +19,33 @@ function getHighestJoltages(bank) {
|
|||||||
// Sort them highest to lowest
|
// Sort them highest to lowest
|
||||||
joltages.sort((a, b) => {
|
joltages.sort((a, b) => {
|
||||||
if (a.value < b.value) return 1;
|
if (a.value < b.value) return 1;
|
||||||
|
else if (a.value === b.value) {
|
||||||
|
if (a.index > b.index) return 1;
|
||||||
|
}
|
||||||
else return -1;
|
else return -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (joltages[0].index == bank.length - 1) {
|
||||||
|
const tmp0 = joltages[0];
|
||||||
|
let tmp1;
|
||||||
|
let nextLowestFound = false;
|
||||||
|
let k = 1;
|
||||||
|
while(nextLowestFound == false) {
|
||||||
|
if (joltages[k].value < joltages[0].value) {
|
||||||
|
tmp1 = joltages[k];
|
||||||
|
nextLowestFound = true;
|
||||||
|
} else {
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
joltages[0] = tmp1;
|
||||||
|
joltages[1] = tmp0;
|
||||||
|
}
|
||||||
|
|
||||||
const finalJoltages = [
|
const finalJoltages = [
|
||||||
joltages[0]
|
joltages[0]
|
||||||
];
|
];
|
||||||
|
|
||||||
let foundValid = false;
|
let foundValid = false;
|
||||||
let j = 1;
|
let j = 1;
|
||||||
while (foundValid == false) {
|
while (foundValid == false) {
|
||||||
@ -39,11 +60,16 @@ function getHighestJoltages(bank) {
|
|||||||
return finalJoltages;
|
return finalJoltages;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = 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++;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user