You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
837 B
34 lines
837 B
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}`); |
|
|
|
|