Public
Edited
Nov 29, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
function countMatches(startA, startB, iterations) {
const [factorA, factorB, divisor] = [16807, 48271, 2147483647];
let [valA, valB] = [startA, startB];
let count = 0;

for (let i = 0; i < iterations; i++) {
valA = (valA * factorA) % divisor;
valB = (valB * factorB) % divisor;
if ((valA & 0xffff) === (valB & 0xffff)) {
count++;
}
}
return count;
}
Insert cell
function part1(input) {
return countMatches(289, 629, 40000000);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function countMatchesWithFilter(startA, startB, iterations) {
const [factorA, factorB, divisor] = [16807, 48271, 2147483647];
let [valA, valB] = [startA, startB];
let count = 0;

for (let i = 0; i < 5000000; i++) {
do {
valA = (valA * factorA) % divisor;
} while (valA & 0b11); // Multiples of 4 only

do {
valB = (valB * factorB) % divisor;
} while (valB & 0b111); // Multiples of 8 only

if ((valA & 0xffff) == (valB & 0xffff)) {
count++;
}
}
return count;
}
Insert cell
function part2(input) {
return countMatchesWithFilter(289, 629, 5000000);
}
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