From ea2449a171230bf5bb79d4cc010aa72e720daf31 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 2 Dec 2022 16:38:10 +0300 Subject: feat(day-2): dumb part 1 solution --- day-2/index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 day-2/index.js (limited to 'day-2/index.js') diff --git a/day-2/index.js b/day-2/index.js new file mode 100644 index 0000000..3671a9b --- /dev/null +++ b/day-2/index.js @@ -0,0 +1,49 @@ +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); + -- cgit v1.2.3