From 0483ec9d32325e1fbf565002aa509b98e9448743 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Fri, 5 Dec 2025 15:33:50 -0500 Subject: [PATCH] Part 1 Done --- src/03.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/03.js b/src/03.js index 63e3bec..28d822e 100644 --- a/src/03.js +++ b/src/03.js @@ -19,12 +19,33 @@ function getHighestJoltages(bank) { // Sort them highest to lowest joltages.sort((a, b) => { if (a.value < b.value) return 1; + 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; + 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 = [ joltages[0] ]; + let foundValid = false; let j = 1; while (foundValid == false) { @@ -39,11 +60,16 @@ function getHighestJoltages(bank) { return finalJoltages; } -let counter = 0; +let counter = 1; + inputs.forEach(bank => { console.log("Bank: ", counter); 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++; });