HATEBIN
>
const fs = require('node:fs') // let rucksacks = fs.readFileSync('input.txt').toString().split('\n'), priority = 0 // A function that returns the priority of an item const getPriority = (letter) => { let characterCodeIndex // Check letter case: if (letter.toLowerCase() === letter) { // Letter is lowercase characterCodeIndex = 96 } else { // Assume letter is uppercase characterCodeIndex = 38 } // Return converted letter to number (priority) return letter.charCodeAt(0) - characterCodeIndex } // rucksacks.forEach(rucksack => { // Split the rucksacks' compartments and store them as an array of items let firstCompartment = rucksack.slice(0, rucksack.length/2).split(''), secondCompartment = rucksack.slice(rucksack.length/2).split(''), matched = false // Inefficiently loop through both of the compartments: firstCompartment.forEach(firstItem => { // If an item matched: if (matched === true) { // Break out of the forEach loop return false } secondCompartment.forEach(secondItem => { // If an item matched: if (matched === true) { // Break out of the forEach loop return false } // If items match: else if (firstItem === secondItem) { matched = true // Add the priority for matched item priority += getPriority(firstItem) } }) }) }) console.log(priority)