Public
Edited
Dec 4, 2023
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
day4_parse_input = input => input
.split("\n")
.map(l => {
const [a, b] = l.slice(5).split(":");
const [winning, numbers] = b.split("|").map(d => d.trim().split(/ +/).map(d => +d.trim()));
const win = new Set(winning);
const matched = numbers.filter(d => win.has(d)).length;
return {card: +a.trim(), winning, numbers, matched};
})
Insert cell
day4_cards = day4_parse_input(day4_input)
Insert cell
day4_part1_result = day4_cards.reduce((s, {matched: v}) => s + (v ? 2 ** (v - 1) : 0), 0)
Insert cell
day4_part2_result = {
const cards = new Map(day4_cards.map(d => ([d.card, d])));
const wins = new Map();
const count = d => {
if(wins.has(d)) return wins.get(d);
let counted = 1;
for(let i = d.card + 1; i < d.card + 1 + d.matched; i++) counted += count(cards.get(i));
wins.set(d, counted);
return counted;
};
return [...cards.values()].map(count).reduce((s, v) => s + v);
}
Insert cell
Insert cell
Insert cell
day3_part1_result = {
const w = day3_input.indexOf("\n") + 1;
// Pad the top and bottom so that we don't have to special-case it.
const empty = ".".repeat(w - 1) + "\n";
const input = empty + day3_input + empty;
const part = i => input[i] !== "." && input[i] !== "\n" && `${+input[i]}` !== input[i];
const seek = (i0, i1) => { for(let i = i0; i <= i1; i++) if(part(i)) return true };
return Array.from(input.matchAll(/\d+/g), ({0: m, index: i}) => {
const i0 = i - 1;
const i1 = i + m.length;
return part(i0) || part(i1) || seek(i0 - w, i1 - w) || seek(i0 + w, i1 + w) ? +m : 0;
}).reduce((s, v) => s + v);
}
Insert cell
day3_part2_result = {
const w = day3_input.indexOf("\n") + 1;
const input = day3_input;
const digit = i => `${+input[i]}` === input[i];
const seek = (i) => {
if(!digit(i)) return;
let i0 = i, i1 = i;
while(digit(i0 - 1)) i0--;
while(digit(++i1));
return [input.slice(i0, i1), i0, i1];
};
const seekRow = (i0, i1) => {
const m = [];
for(let i = i0, n; i <= i1; i++) if(n = seek(i)) m.push(n), i = n[2];
return m;
}
return Array.from(input.matchAll(/[*]/g), ({index: i}) => {
const [a, b, c] = [
...seekRow(i - 1, i - 1),
...seekRow(i + 1, i + 1),
...seekRow(i - 1 - w, i + 1 - w),
...seekRow(i - 1 + w, i + 1 + w),
];
return b && !c ? a[0] * b[0] : 0;
}).reduce((s, v) => s + v);
}
Insert cell
Insert cell
Insert cell
day2_games = day2_input
.split("\n")
.map(l => l.slice(5).split(":"))
.map(([a, b]) => ({
id: +a,
draws: b.split(";").map(round => ({
red: 0, green: 0, blue: 0,
...Object.fromEntries(
round.trim().split(", ").map(d => d.split(" ")).map(([a, b]) => [b, +a])
),
}))
}))
Insert cell
day2_part1_result = {
const limits = Object.entries({red: 12, green: 13, blue: 14});
return day2_games
.filter(
({draws}) => draws.every(draw => limits.every(([color, max]) => draw[color] <= max))
)
.reduce((s, {id}) => s + id, 0);
}
Insert cell
day2_part2_result = day2_games
.map(({draws}) => ["red", "green", "blue"].map(
color => Math.max(...draws.map(d => d[color]))
).reduce((f, v) => f * v, 1)
).reduce((s, v) => s + v)
Insert cell
Insert cell
Insert cell
day1_part1_result = day1_input
.split("\n")
.map(l => Array.from(l).filter(isFinite))
.reduce((s, d) => s + +(d[0] + d.at(-1)), 0)
Insert cell
day1_part2_result = {
const words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
const length = Math.max(...words.map(s => s.length));
const match = new RegExp(words.join("|"));
// To account for overlapping words we perform a lookahead which does not consume the match.
return day1_input
.split("\n")
.map(line => Array.from(
line.matchAll(new RegExp(`(?=${words.join("|")}|\\d)`, "g")),
({index: i, input: s}) => isNaN(s[i])
? String(words.indexOf(s.slice(i, i + length).match(match)[0]) + 1)
: s[i]
))
.reduce((s, l) => s + +(l[0] + l.at(-1)), 0)
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more