Skip to content

Commit

Permalink
2a
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake Thomas committed Dec 2, 2022
1 parent 72acf09 commit 51363a2
Show file tree
Hide file tree
Showing 3 changed files with 2,574 additions and 0 deletions.
71 changes: 71 additions & 0 deletions two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { readFile } = require('fs/promises');

// 2a
async function init() {
const input = await readFile('two.txt', 'utf8');
// const input = await readFile('two.test.txt', 'utf8');
const data = input.split('\n');

let score = 0;

data.forEach((d) => {
const [opp, me] = d.split(' ');

score += play(opp, me);
score += value(me);
});

console.log(score);
}

function play(opp, me) {
if (me === 'X') {
if (opp === 'A') {
return 3;
}
if (opp === 'B') {
return 0;
}
if (opp === 'C') {
return 6;
}
}
if (me === 'Y') {
if (opp === 'A') {
return 6;
}
if (opp === 'B') {
return 3;
}
if (opp === 'C') {
return 0;
}
}
if (me === 'Z') {
if (opp === 'A') {
return 0;
}
if (opp === 'B') {
return 6;
}
if (opp === 'C') {
return 3;
}
}
}

function value(me) {
if (me === 'X') {
return 1;
}
if (me === 'Y') {
return 2;
}
if (me === 'Z') {
return 3;
}

throw new Error('bad me', me);
}

init();
3 changes: 3 additions & 0 deletions two.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A Y
B X
C Z
Loading

0 comments on commit 51363a2

Please sign in to comment.