Public
Edited
Sep 4, 2022
2 forks
1 star
Insert cell
Insert cell
Insert cell
function parse(input) {
return AOC.chunk(input.split(/\s+/).map(Number), 25);
}
Insert cell
Insert cell
Insert cell
function mark(cards, ball) {
cards.forEach((card) =>
card.forEach((n, i) => (card[i] = n === ball ? null : card[i]))
);
}
Insert cell
Insert cell
function markedCard(card, pos) {
const markedRow =
card.filter((n, i) => Math.floor(i / 5) === pos && n != null).length === 0;
const markedCol =
card.filter((n, i) => i % 5 === pos && n != null).length === 0;
return markedRow || markedCol;
}
Insert cell
Insert cell
function checkBingo(cards) {
for (const card of cards) {
for (let pos = 0; pos < 5; pos++) {
if (markedCard(card, pos)) {
return card;
}
}
}
return null;
}
Insert cell
Insert cell
function part1(input) {
const cards = parse(input.cards);

for (const n of input.ns) {
mark(cards, n);
const bingoCard = checkBingo(cards);
if (bingoCard != null) {
return AOC.sum(bingoCard) * n;
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function checkBingo2(cards, completedCards) {
let completedCard = null;
for (const card of cards) {
if (!completedCards.has(card)) {
for (let pos = 0; pos < 5; pos++) {
if (markedCard(card, pos)) {
completedCards.add(card);
}
}
completedCard = card;
}
}
return completedCard;
}
Insert cell
Insert cell
function part2(input) {
const cards = parse(input.cards);
const completedCards = new Set();

for (const n of input.ns) {
mark(cards, n);
const bingoCard = checkBingo2(cards, completedCards);
if (completedCards.size === cards.length) {
return AOC.sum(bingoCard) * n;
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
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