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, } const shapeMap = { A: ROCK, B: PAPER, C: SCISSORS, X: ROCK, Y: PAPER, Z: SCISSORS, } const shapeScores = { [ROCK]: 1, [PAPER]: 2, [SCISSORS]: 3, }; const outcomeScores = { [LOSS]: 0, [DRAW]: 3, [WIN]: 6 }; const result = lines.reduce((acc, line) => { const [opponentShape, shape] = line.split(' ').map(code => shapeMap[code]); if (!shape || !opponentShape) return acc; let outcome = LOSS; if (shape === opponentShape) outcome = DRAW; else if (defeatMap[shape] === opponentShape) outcome = WIN; return acc + shapeScores[shape] + outcomeScores[outcome]; }, 0) console.log(result);