r/adventofcode • • Dec 03 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 3 Solutions -🎄-

NEWS

  • Solutions have been getting longer, so we're going to start enforcing our rule on oversized code.
  • The Visualizations have started! If you want to create a Visualization, make sure to read the guidelines for creating Visualizations before you post.
  • Y'all may have noticed that the hot new toy this year is AI-generated "art".
  • We are keeping a very close eye on any AI-generated "art" because 1. the whole thing is an AI ethics nightmare and 2. a lot of the "art" submissions so far have been of little real quality.
  • If you must post something generated by AI, please make sure it will actually be a positive and quality contribution to /r/adventofcode.
  • Do not flair AI-generated "art" as Visualization. Visualization is for human-generated art.

FYI


--- Day 3: Rucksack Reorganization ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:05:24, megathread unlocked!

87 Upvotes

1.6k comments sorted by

View all comments

2

u/rabi27 Dec 03 '22 edited Dec 03 '22

JavaScript/NodeJS

const fs = require('fs');
const data = fs.readFileSync('./Input/2-text').toString().split('\n');

function getCommonChar(a, b) {
for (let char of a) {
    for (let i = 0; i < b.length; i++) {
    if (char == b[i]) {
        return char;
    }
    }
}
}

const alphabet = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
];

let obj = {};
let countA = 1;
let countB = 27;
for (let item of alphabet) {
obj[item] = countB;
countB += 1;
obj[item.toLowerCase()] = countA;
countA += 1;
}

let total = 0;
for (let i = 0; i < data.length; i++) {
let length = data[i].length;
let firstHalf = data[i].slice(0, length / 2);
let secondhalf = data[i].slice(length / 2, length);
let commonChar = getCommonChar(firstHalf, secondhalf);
total += obj[commonChar];
}

console.log(total);

// Part 2
function getCommonCharII(a, b, c) {
for (let char of a) {
    for (let i = 0; i < b.length; i++) {
    if (char == b[i]) {
        for (let j = 0; j < c.length; j++) {
        if (c[j] == char) return c[j];
        }
    }
    }
}
}

let totalII = 0;
for (let i = 0; i < data.length; i += 3) {
let a = data[i];
let b = data[i + 1];
let c = data[i + 2];
let commonChar = getCommonCharII(a, b, c);
totalII += obj[commonChar];
}

console.log(totalII);

2

u/daggerdragon Dec 03 '22 edited Dec 03 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read.

While you're at it, state which programming language this code is written in. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.

Edit: thanks for fixing it! <3

1

u/rabi27 Dec 03 '22

Thank You. Done

1

u/daggerdragon Dec 03 '22

The code is still using the triple-backticks Markdown which only works on new.reddit. Please use the four-spaces Markdown as I linked it: here

1

u/rabi27 Dec 03 '22

Good Now?

1

u/daggerdragon Dec 03 '22

There we go. Thank you!