Public
Edited
Dec 2, 2022
1 fork
1 star
Insert cell
Insert cell
playRPS(testData)
Insert cell
playRPS(await FileAttachment("aoc-input-02.txt").text())
Insert cell
Insert cell
function numberFromLetter(letter) {
switch(letter) {
case 'A':
case 'X':
return 1;
case 'B':
case 'Y':
return 2;
default:
return 3;
}
}
Insert cell
function win(me, other) {
return (me > other) && !(me === 3 && other === 1);
}
Insert cell
function playRPS(input) {
const lines = input.split('\n');

let score = 0;
for (let line of lines) {
const other = numberFromLetter(line[0]);
const me = numberFromLetter(line[2]);

if (me === other) {
score += me + 3;
} else {
if (win(me, other)) {
score += me + 6;
} else {
score += me;
}
}
}

return score;
}
Insert cell
testData = `A Y
B X
C Z`
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more