const fs = require('fs'); const input = fs.readFileSync('./day-2/input.txt').toString(); const lines = input.split('\n'); const [ROCK, PAPER, SCISSORS] = ['rock', 'paper', 'scissors']; const [LOSS, WIN, DRAW] = ['loss', 'win', 'draw']; const defeatMap = { // What defeats what [ROCK]: SCISSORS, [SCISSORS]: PAPER, [PAPER]: ROCK, } // This is dumb const suckMap = { // What loses to what [ROCK]: PAPER, [SCISSORS]: ROCK, [PAPER]: SCISSORS, } const inputMap = { A: ROCK, B: PAPER, C: SCISSORS, X: LOSS, Y: DRAW, Z: WIN, } const shapeScores = { [ROCK]: 1, [PAPER]: 2, [SCISSORS]: 3, }; const outcomeScores = { [LOSS]: 0, [DRAW]: 3, [WIN]: 6 }; const result = lines.reduce((acc, line) => { const [opponentShape, outcome] = line.split(' ').map(code => inputMap[code]); if (!outcome || !opponentShape) return acc; let shape; if (outcome === DRAW) shape = opponentShape; else if (outcome === LOSS) shape = defeatMap[opponentShape]; else if (outcome === WIN) shape = suckMap[opponentShape]; return acc + shapeScores[shape] + outcomeScores[outcome]; }, 0) console.log(result);