const fs = require("fs");
const path = require("path");
const outcome = {
WIN: 6,
DRAW: 3,
LOSE: 0,
};
const GAME = {
figures: {
X: 1, // Rock
Y: 2, // Paper
Z: 3, // Scissors
},
result: {
A: {
// Rock
Y: outcome.WIN,
X: outcome.DRAW,
Z: outcome.LOSE,
},
B: {
// Paper
Z: outcome.WIN,
Y: outcome.DRAW,
X: outcome.LOSE,
},
C: {
// Scissors
X: outcome.WIN,
Z: outcome.DRAW,
Y: outcome.LOSE,
},
},
play: {
A: {
Z: "Y",
Y: "X",
X: "Z",
},
B: {
Z: "Z",
Y: "Y",
X: "X",
},
C: {
Z: "X",
Y: "Z",
X: "Y",
},
},
};
const getPoints = (opp, myPlay) =>
GAME.figures[myPlay] + GAME.result[opp][myPlay];
const getResult = (exercise) =>
fs
.readFileSync(path.resolve(__dirname, "input.txt"), "utf8")
.split("\n")
.reduce((acc, current) => {
const play = current.split(" ");
const myPlay = exercise === 1 ? play[1] : GAME.play[play[0]][play[1]];
return acc + getPoints(play[0], myPlay);
}, 0);
console.log(getResult(1), getResult(2));