Advent-of-Code-2022/2/solution.mjs

35 lines
837 B
JavaScript
Raw Normal View History

2022-12-02 11:23:21 -08:00
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}`);