Public
Edited
Dec 4
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
return input.split("\n").map((row) => row.split(""));
}
Insert cell
Insert cell
dirs8 = [
[-1, -1],
[0, -1],
[1, -1],
[-1, 0],
[1, 0],
[-1, 1],
[0, 1],
[1, 1]
]
Insert cell
Insert cell
function getStr(grid, row, col, dirs, len, offset = 0) {
return dirs
.map(([dr, dc]) => {
const str = d3
.range(len)
.map(
(i) => grid[row + (i - offset) * dr]?.[col + (i - offset) * dc] ?? ""
)
.join("");
return str.length === len ? str : "";
})
.filter((str) => str !== "");
}
Insert cell
Insert cell
function part1(input) {
const grid = parse(input);

const wordCount = (r, c, chr) =>
chr === "X"
? getStr(grid, r, c, dirs8, 4).filter((d) => d === "XMAS").length
: 0;

return d3.sum(AOC.gMapWithLocation(wordCount, grid).flat());
}
Insert cell
Insert cell
Insert cell
Insert cell
dirs4 = [
[-1, -1],
[1, -1],
[-1, 1],
[1, 1]
]
Insert cell
function part2(input) {
const grid = parse(input);

const cross = (r, c, chr) =>
chr === "A" &&
getStr(grid, r, c, dirs4, 3, 1).filter((d) => d === "MAS").length === 2;

return d3.sum(AOC.gMapWithLocation(cross, grid).flat());
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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