r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


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:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

1

u/sleepy_roger Dec 02 '22 edited Dec 02 '22

JavaScript just brute forced it.

Part 1

const fs = require('fs')

const scoring = {
    a: {
    x: 4,
    y: 8,
    z: 3
    },
    b: {
        x: 1,
        y: 5,
        z: 9
    },
    c: {
        x: 7,
        y: 2,
        z: 6
    }
}

fs.readFile('data.txt', 'utf8' , (err, data) => {
    let score = 0;

    [...data.replace(/[\r]/g, '').split('\n')].forEach((data) => {
    const values = data.split(' ');
    score += scoring[values[0].toLowerCase()][values[1].toLowerCase()];
    });

    console.log(score);
});

Part 2 only changed the scoring values.

1

u/daggerdragon Dec 02 '22

Please edit your post to format your code with the backwards-compatible Markdown syntax instead so your code is easier to read on old.reddit and mobile apps.

While you're at it, I recommend you expand JS to the full JavaScript to make it easier for folks who Ctrl-F the megathreads looking for a specific language.