Public
Edited
Dec 2, 2022
Insert cell
Insert cell
Insert cell
parse = (input) => input.split("\n").map((r) => r.split(" "))
Insert cell
parse(testInput)
Insert cell
willIWin = ( a, b ) =>
( a === 'A' && b === 'Y' ) ||
( a === 'B' && b === 'Z' ) ||
( a === 'C' && b === 'X' )
Insert cell
[willIWin("A", "Y"), willIWin("B", "X"), willIWin("C", "Z")]
Insert cell
willITie = ( a, b ) =>
( a === 'A' && b === 'X' ) ||
( a === 'B' && b === 'Y' ) ||
( a === 'C' && b === 'Z' )
Insert cell
[willITie("A", "Y"), willITie("B", "X"), willITie("C", "Z")]
Insert cell
shapeScores = ({
A: 1,
X: 1,
B: 2,
Y: 2,
C: 3,
Z: 3
})
Insert cell
scoreRound = ( a, b ) => {
if ( willIWin( a, b ) ) return shapeScores[b] + 6;
if ( willITie( a, b ) ) return shapeScores[b] + 3;
return shapeScores[b];
}
Insert cell
parse(testInput).map((round) => scoreRound(round[0], round[1]))
Insert cell
part1 = ( input ) => parse( input ).map((round) => scoreRound(round[0], round[1])).reduce( ( acc, score ) => acc + score )
Insert cell
part1(testInput)
Insert cell
Insert cell
part1(input)
Insert cell
shapeTie = ({ A: "X", B: "Y", C: "Z" })
Insert cell
shapeWin = ({ A: "Y", B: "Z", C: "X" })
Insert cell
shapeLose = ({ A: 'Z', B: 'X', C: 'Y' })
Insert cell
strategyMap = ({X: shapeLose, Y: shapeTie, Z: shapeWin})
Insert cell
myPick = (oppShape, outcome) => strategyMap[outcome][oppShape]
Insert cell
parse(testInput).map( r => myPick(r[0], r[1] ))
Insert cell
part2 = (input) =>
parse(input)
.map((r) => [r[0], myPick(r[0], r[1])])
.map((round) => scoreRound(round[0], round[1]))
.reduce((acc, score) => acc + score)
Insert cell
part2(testInput)
Insert cell
part2(input)
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