Solutions to day 2

main
Eric Wagoner 4 months ago
parent 8764826a99
commit ca219ff321
  1. 101
      02/code.js
  2. 2500
      02/input.txt

@ -0,0 +1,101 @@
const fs = require('fs');
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
// Part One
let totalScore = 0;
for (i in inputArray) {
const value = (inputArray[i]);
const throws = value.split(" ");
const result = getResult(throws[0], throws[1]);
const score = getScore(result, throws[1]);
totalScore += score;
}
console.log(totalScore);
// Part Two
totalScore = 0;
for (i in inputArray) {
const value = (inputArray[i]);
const throws = value.split(" ");
const result = getThrownResult(throws[1]);
const thrownThrow = getThrownThrow(throws[0], throws[1]);
const score = result + thrownThrow;
totalScore += score;
}
console.log(totalScore);
// functions
function getResult(them, me) {
let result;
if (them === "A") {
if (me === "X") {
result = 1;
} else {
result = me === "Y" ? 2 : 0;
}
}
if (them === "B") {
if (me === "Y") {
result = 1;
} else {
result = me === "Z" ? 2 : 0;
}
}
if (them === "C") {
if (me === "Z") {
result = 1;
} else {
result = me === "X" ? 2 : 0;
}
}
return result * 3;
}
function getScore(result, me) {
const options = ["X", "Y", "Z"];
const value = options.indexOf(me) + 1;
return value + result;
}
function getThrownResult(thrown) {
const results = ["X", "Y", "Z"];
return results.indexOf(thrown) * 3;
}
function getThrownThrow(them, me) {
let result;
if (them === "A") {
if (me === "Y") {
result = 1;
} else {
result = me === "X" ? 3 : 2;
}
}
if (them === "B") {
if (me === "Y") {
result = 2;
} else {
result = me === "X" ? 1 : 3;
}
}
if (them === "C") {
if (me === "Y") {
result = 3;
} else {
result = me === "X" ? 2 : 1;
}
}
return result;
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save