Public
Edited
Dec 8, 2022
1 fork
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const input = parse(inputs.real);
let part1Scores = input
.map((d) => scoreMatch(...d))
.map((score, index) => ({ score, part: "part1", index }));
let part2Scores = input
.map(([a, b]) => scoreMatch(a, partTwoShouldPlay(a, b)))
.map((score, index) => ({ score, part: "part2", index }));
let data = [...part1Scores, ...part2Scores];
return Plot.plot({
color: { legend: true },
marks: [
Plot.ruleY([0]),
Plot.lineY(
data,
Plot.mapY("cumsum", { x: "index", y: "score", stroke: "part" })
)
]
});
}
Insert cell
inputs = ({
test: `A Y\nB X\nC Z`,
real: await FileAttachment("input").text()
})
Insert cell
symbolMap = ({
A: "rock",
X: "rock",
B: "paper",
Y: "paper",
C: "scissors",
Z: "scissors"
})
Insert cell
resultMap = ({ X: "lose", Y: "tie", Z: "win" })
Insert cell
parse = aoc.lines((d) => d.split(/\s+/))
Insert cell
points = ({ rock: 1, paper: 2, scissors: 3 })
Insert cell
function scoreMatch(theyPlay, iPlay) {
if (iPlay == "X" && theyPlay == "A") return 1 + 3;
if (iPlay == "X" && theyPlay == "B") return 1 + 0;
if (iPlay == "X" && theyPlay == "C") return 1 + 6;
if (iPlay == "Y" && theyPlay == "A") return 2 + 6;
if (iPlay == "Y" && theyPlay == "B") return 2 + 3;
if (iPlay == "Y" && theyPlay == "C") return 2 + 0;
if (iPlay == "Z" && theyPlay == "A") return 3 + 0;
if (iPlay == "Z" && theyPlay == "B") return 3 + 6;
if (iPlay == "Z" && theyPlay == "C") return 3 + 3;
throw new Error(`idk about ${theyPlay} vs ${iPlay}`);
}
Insert cell
function partTwoShouldPlay(theyPlay, result) {
if (result == "X" && theyPlay == "A") return "Z";
if (result == "X" && theyPlay == "B") return "X";
if (result == "X" && theyPlay == "C") return "Y";
if (result == "Y" && theyPlay == "A") return "X";
if (result == "Y" && theyPlay == "B") return "Y";
if (result == "Y" && theyPlay == "C") return "Z";
if (result == "Z" && theyPlay == "A") return "Y";
if (result == "Z" && theyPlay == "B") return "Z";
if (result == "Z" && theyPlay == "C") return "X";
throw new Error(`idk about ${theyPlay} to get ${result}`);
}
Insert cell
function part1(input) {
return d3.sum(input.map(([a, b]) => scoreMatch(a, b)));
}
Insert cell
function part2(input) {
return d3.sum(input.map(([a, b]) => scoreMatch(a, partTwoShouldPlay(a, b))));
}
Insert cell
expected = ({
test: [15, 12],
real: [10624, 14060]
})
Insert cell
meta = aoc.meta({
day: 2,
parse,
inputs,
parts: [part1, part2],
expected,
href: "https://observablehq.com/d/9078c111178af6cd?collection=@mythmon/advent-of-code-2022"
})
Insert cell
import {aoc} from "@mythmon/aoc-helpers"
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