Public
Edited
Dec 7, 2022
Insert cell
Insert cell
rawData = FileAttachment("aocdata-2").text()
Insert cell
Insert cell
scoreTable = [
// You played...
// R P S
[1 + 3, 2 + 6, 3 + 0], // They played rock
[1 + 0, 2 + 3, 3 + 6], // They played paper
[1 + 6, 2 + 0, 3 + 3] // They played scissors
]
Insert cell
modifiedData = rawData.replaceAll(/A|B|C|X|Y|Z/g, (d) => {
if (d == "A" || d == "X") {
return 0;
} else if (d == "B" || d == "Y") {
return 1;
} else if (d == "C" || d == "Z") {
return 2;
}
})
Insert cell
plays = modifiedData
.split("\n")
.map((d) => d.split(" "))
.slice(0, -1)
Insert cell
scores = plays.map((d, i) => scoreTable[plays[i][0]][plays[i][1]])
Insert cell
totalScore = scores.reduce(
(accumulator, currentValue) => accumulator + currentValue
)
Insert cell
Insert cell
scoreTablePart2 = [
// Play bonuses: rock = 1, Paper = 2, Scissors = 3
// You should...
// win draw lose
[6 + 2, 3 + 1, 0 + 3], // They played rock (win: paper, draw: rock, lose: scissors)
[6 + 3, 3 + 2, 0 + 1], // They played paper (win: scissors, draw: paper, lose: rock)
[6 + 1, 3 + 3, 0 + 2] // They played scissors (win: rock, draw: scissors, lose: paper)
]
Insert cell
modifiedDataPart2 = rawData.replaceAll(/A|B|C|X|Y|Z/g, (d) => {
if (d == "A" || d == "Z") {
// Z should win, which is the first column above
return 0;
} else if (d == "B" || d == "Y") {
return 1;
} else if (d == "C" || d == "X") {
// X should lose, which is the third column above
return 2;
}
})
Insert cell
playsPart2 = modifiedDataPart2
.split("\n")
.map((d) => d.split(" "))
.slice(0, -1)
Insert cell
scoresPart2 = playsPart2.map(
(d, i) => scoreTablePart2[playsPart2[i][0]][playsPart2[i][1]]
)
Insert cell
totalScorePart2 = scoresPart2.reduce(
(accumulator, currentValue) => accumulator + currentValue
)
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