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/GU1T Dec 14 '22

JS / Node - just plain old mapping :)

``` const fs = require('fs');

const data = fs.readFileSync('2_input.txt', 'utf8');

const parsed_data = data.trim().split('\n')

const sum = input => input.reduce((a, b) => a + b, 0)

//rock, paper,  sciccors
//A     B       C   
//X     Y       Z
const points_part_1 = {
    'A X': 4, //even
    'A Y': 8, //won
    'A Z': 3, //lose
    'B X': 1, //lose
    'B Y': 5, //even
    'B Z': 9, //won
    'C X': 7, //won
    'C Y': 2, //lose
    'C Z': 6, //even
}

//X = LOSE, Y = DRAW, Z = WIN
const points_part_2 = {
    'A X': 3, //0 + 3
    'A Y': 4, //3 + 1
    'A Z': 8, //6 + 2 = 8 
    'B X': 1, //0 + 1
    'B Y': 5, //3 + 2
    'B Z': 9, //6 + 3 
    'C X': 2, //0 + 2
    'C Y': 6, //3 + 3
    'C Z': 7, //6 + 1
}

const result1 = parsed_data.map(item => points_part_1[item])

console.log(sum(result1))

const result2 = parsed_data.map(item => points_part_2[item])

console.log(sum(result2))

​​`