finished part 2 of day 2

This commit is contained in:
Robert McGovern 2022-12-04 21:20:24 +00:00
parent 1c171f77bd
commit 2c1151fe33
1 changed files with 141 additions and 0 deletions

141
aoc/2022/day2/d2c2.js Normal file
View File

@ -0,0 +1,141 @@
"use strict";
const fs = require("fs");
const inputText = fs.readFileSync("./input.txt").toString("utf-8");
const splitInputText = inputText.split("\n");
const splitTestData = `
A Y
B X
C Z
`.split("\n");
function processData(input) {
let currentScore = 0;
input.forEach((line) => {
if (line.length > 0) {
//add line check the moves are ABC / XYZ if not skip line
const playerOneMove = convertMove(line[0]);
const playerTwoMove = calculatePlayerMove(playerOneMove, line[2]);
// console.log(playerTwo);
const score = calculateScoreForRound(playerOneMove, playerTwoMove);
currentScore += score;
}
});
return currentScore;
}
/**
* [calculatePlayerMove description]
*
* @param {String} playerMove [playerMove description]
* @param {String} outcome [outcome description]
*
* @return {String} [return description]
*/
function calculatePlayerMove(playerMove, outcome) {
let calculatedMove = "";
// WIN
if (outcome === "Z") {
switch (playerMove) {
case "rock":
calculatedMove = "paper";
break;
case "paper":
calculatedMove = "scissors";
break;
case "scissors":
calculatedMove = "rock";
break;
}
} else if (outcome === "Y") {
//DRAW
calculatedMove = playerMove;
} else if (outcome === "X") {
// LOSE
switch (playerMove) {
case "rock":
calculatedMove = "scissors";
break;
case "paper":
calculatedMove = "rock";
break;
case "scissors":
calculatedMove = "paper";
break;
}
}
return calculatedMove;
}
/**
* [convertMove description]
*
* @param {String} input [input description]
*
* @return {String} [return description]
*/
function convertMove(input) {
let move = "";
let santatizedInput = input.toUpperCase();
// console.log(santatizedInput);
switch (santatizedInput) {
case "A":
case "X":
move = "rock";
break;
case "B":
case "Y":
move = "paper";
break;
case "C":
case "Z":
move = "scissors";
break;
}
return move;
}
function calculateScoreForRound(playerOneMove, playerTwoMove) {
let score = 0;
switch (playerTwoMove) {
case "rock":
score = 1;
break;
case "paper":
score = 2;
break;
case "scissors":
score = 3;
break;
}
if (playerOneMove === playerTwoMove) {
// draw
score += 3;
} else if (
(playerOneMove === "rock" && playerTwoMove === "scissors") ||
(playerOneMove === "paper" && playerTwoMove === "rock") ||
(playerOneMove === "scissors" && playerTwoMove === "paper")
) {
// loss player 1
} else {
// win for player 2
score += 6;
}
console.log(`P1: ${playerOneMove} P2:${playerTwoMove} = ${score}`);
return score;
}
console.log("TestData, Expected 12 got " + processData(splitTestData));
console.log("Real Data, got " + processData(splitInputText));