diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-12-02 16:38:10 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-12-02 16:51:41 +0300 |
commit | ea2449a171230bf5bb79d4cc010aa72e720daf31 (patch) | |
tree | 57d122ccb4958c195921214f451aa2e1738b4491 /day-2/index.js | |
parent | 8d915727b98c41ca25de745a01f9d56df95d070d (diff) | |
download | aoc-2022-ea2449a171230bf5bb79d4cc010aa72e720daf31.tar.gz |
feat(day-2): dumb part 1 solution
Diffstat (limited to 'day-2/index.js')
-rw-r--r-- | day-2/index.js | 49 |
1 files changed, 49 insertions, 0 deletions
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); + |