finished part 1 of day 2

This commit is contained in:
Robert McGovern 2022-12-04 20:59:12 +00:00
parent 8bc861d73c
commit 1c171f77bd
2 changed files with 2596 additions and 0 deletions

96
aoc/2022/day2/d2c1.js Normal file
View File

@ -0,0 +1,96 @@
"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 = convertMove(line[2]);
// console.log(playerTwo);
const score = calculateScoreForRound(playerOneMove, playerTwoMove);
currentScore += score;
}
});
return currentScore;
}
/**
* [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 15 got " + processData(splitTestData));
console.log("Real Data, got " + processData(splitInputText));

2500
aoc/2022/day2/input.txt Normal file

File diff suppressed because it is too large Load Diff