Advent-of-Code-2022/2/solution.mjs
2022-12-02 12:23:21 -07:00

35 lines
837 B
JavaScript

import { readFileSync } from 'node:fs';
const input = readFileSync('input', 'utf-8');
const data = input
.split('\n')
.filter(line => line)
.map(line => {
const play = line.split(' ');
return [
['A', 'B', 'C'].indexOf(play[0]),
['X', 'Y', 'Z'].indexOf(play[1])
]
});
const scoreGame = plays =>
plays.map(([o, p]) =>
(p + 1) + // shape score
3 * (o == p) + // draw
6 * ((o + 1) % 3 == p) // win
);
const wrongScores = scoreGame(data);
const totalScore = wrongScores.reduce((t, s) => t + s, 0)
console.log(`Total Score: ${totalScore}`);
const actualPlays = data.map(([o, a]) => [
o,
(o + [2, 0, 1][a]) % 3
]);
const correctScores = scoreGame(actualPlays);
const correctTotalScore = correctScores.reduce((t, s) => t + s, 0)
console.log(`Correct Total Score: ${correctTotalScore}`);