summaryrefslogtreecommitdiff
path: root/day-2/index.js
blob: 245acb7c27d18c7f563aa72f977df69e21a11751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);