re: AoC Day 2: Inventory Management System VIEW POST

FULL DISCUSSION
 

Thanks for hosting the private leaderboard! Never been on a leaderboard before lol so that'll be fun. :)

I am curious - how is everyone posting their code? Is there a code tag on here, like there is on Slack? Is everyone sharing screenshots? I haven't posted a whole lot on here yet, so I'm not sure of the best way to share code.

I'm using JS this year, so here's my day 2 solutions: not the prettiest / most succinct, but they work!

Part 1:

let countDouble = 0;
let countTriple = 0;

for (let i = 0; i < input.length; i++) {
    let label = input[i].split('');
    let letterCount = {};

    label.reduce((letters, letter) => {
        if (letter in letterCount) {
            letterCount[letter]++;
        } else {
            letterCount[letter] = 1;
        }
        return letters;
    }, 0);

    let checkCounts = Object.values(letterCount);
    if (checkCounts.includes(2)) {
        countDouble++;
    }
    if (checkCounts.includes(3)) {
        countTriple++;
    }
}

let checksum = countDouble * countTriple;
console.log(checksum);

Part 2:

for (let i = 0; i < input.length; i++) {
    let root = input[i];

    for (let j = i+1; j < input.length; j++) {
        let currName = input[j];

        if (compareNames(root, currName)) {
            return;
        }
    }
}

function compareNames(first, second) {
    let differences = 0;
    let locations = [];
    for (let k = 0; k < first.length; k++) {
        if (first[k] === second[k]) {
            continue;
        } else {
            differences++;
            locations.push(k);
        }
    }

    if (differences === 1) {
        const letterArray = first.split('');
        const removeLetter = letterArray.splice(locations[0], 1);
        const matchingLetters = letterArray.join('');
        console.log(`same letters: ${matchingLetters}`);
        return true;
    } else {
        return false;
    }

    differences = 0;
    locations = [];
}
 

There's an enhanced form of markdown for code blocks: triple backticks for start and end, and if you immediately follow the opening backticks with the language you get syntax highlighting. Difficult to show raw markdown in markdown unfortunately.

 

Excellent, thank you! Much better than screenshots. :)

code of conduct - report abuse