advent-of-code-2022/day02/index.js

92 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-12-02 09:00:43 +00:00
import { readFileSync } from "fs";
const input = readFileSync("rps.txt", "utf-8");
let totalScore = 0;
// A X rock 1
// B Y paper 2
// C Z scissors 3
// lose 0
// draw 3
// win 6
input.split("\n").forEach(line => {
const arr = line.split(" ");
totalScore += getScore(arr[0], arr[1]);
});
console.log(totalScore);
function getScore(opponent, player) {
let score = 0;
switch(player) {
case "X":
2022-12-02 09:04:11 +00:00
//score += 1;
score += 0;
2022-12-02 09:00:43 +00:00
break;
case "Y":
2022-12-02 09:04:11 +00:00
//score += 2;
score += 3;
2022-12-02 09:00:43 +00:00
break;
case "Z":
2022-12-02 09:04:11 +00:00
//score += 3;
score += 6;
2022-12-02 09:00:43 +00:00
break;
}
switch(opponent) {
case "A":
switch(player) {
case "X":
2022-12-02 09:04:11 +00:00
//score += 3;
2022-12-02 09:00:43 +00:00
score += 3;
break;
case "Y":
2022-12-02 09:04:11 +00:00
//score += 6;
score += 1;
2022-12-02 09:00:43 +00:00
break;
case "Z":
2022-12-02 09:04:11 +00:00
//score += 0;
score += 2;
2022-12-02 09:00:43 +00:00
break;
}
break;
case "B":
switch(player) {
case "X":
2022-12-02 09:04:11 +00:00
score += 1;
2022-12-02 09:00:43 +00:00
break;
case "Y":
2022-12-02 09:04:11 +00:00
score += 2;
2022-12-02 09:00:43 +00:00
break;
case "Z":
2022-12-02 09:04:11 +00:00
score += 3;
2022-12-02 09:00:43 +00:00
break;
}
break;
case "C":
switch(player) {
case "X":
2022-12-02 09:04:11 +00:00
//score += 6;
score += 2;
2022-12-02 09:00:43 +00:00
break;
case "Y":
2022-12-02 09:04:11 +00:00
//score += 0;
score += 3;
2022-12-02 09:00:43 +00:00
break;
case "Z":
2022-12-02 09:04:11 +00:00
//score += 3;
score += 1;
2022-12-02 09:00:43 +00:00
break;
}
break;
}
return score;
}