Published
Edited
Dec 14, 2020
Insert cell
Insert cell
Insert cell
testInput = ({ earliest: 939, ids: [7, 13, 59, 31, 19] })
Insert cell
function q1(input) {
const { earliest, ids } = input;
let minDep = Infinity;
let minId = NaN;
for (const id of ids) {
// skip x
if (Number.isNaN(id)) continue;
const nextDeparture = Math.ceil(earliest / id) * id;

if (nextDeparture < minDep) {
minDep = nextDeparture;
minId = id;
}
}
return {
minDep,
minId,
answer: (minDep - earliest) * minId
};
}
Insert cell
test1 = q1(testInput)
Insert cell
answer1 = q1(input)
Insert cell
Insert cell
function* q2(input) {
let { ids, expected } = input;
if (!expected) expected = undefined;
/*
We basically need to find t such that

t % ids[i].val === ids[i].offset

.. for all i.
*/

// Filter out unimportant ids
ids = ids
.map((val, offset) => ({
val: Number.isNaN(val) ? 0 : BigInt(val),
offset: BigInt(offset)
}))
.filter(({ val, offset }) => val);

let { val: v1, offset: o1 } = ids[0];
let n1 = v1;
let steps = [{ v1, n1 }];
for (let i = 1; i < ids.length; i++) {
let { val: v2, offset: o2 } = ids[i];
let n2 = 0n;
while ((n1 + o2) % v2 !== 0n) {
n1 += v1;
yield { n1, v1, o1, n2, v2, o2 };
}
steps.push({ n1, v1, n2, v2, o2 });
yield steps;

v1 = v1 * v2;
}
yield { steps, answer: n1, expected };
}
Insert cell
Insert cell
testInputs2.map(q2)[4]
Insert cell
q2(input)
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