Public
Edited
Dec 11, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
testMonkeys = parseMonkeys(test)
Insert cell
function parseMonkeys(input) {
return input.trim().split("\n\n").map(parseMonkey);
}
Insert cell
function parseMonkey(input) {
const lines = input.split("\n");
const {groups: {id}} = /^Monkey (?<id>\d+):$/.exec(lines[0]);
const {groups: {items}} = /^ Starting items: (?<items>(?:\d+(, \d+)*))$/.exec(lines[1]);
const {groups: {operation}} = /^ Operation: new = (?<operation>.*)$/.exec(lines[2]);
const {groups: {divisor}} = /^ Test: divisible by (?<divisor>\d+)$/.exec(lines[3]);
const {groups: {left}} = /^ If true: throw to monkey (?<left>\d+)$/.exec(lines[4]);
const {groups: {right}} = /^ If false: throw to monkey (?<right>\d+)$/.exec(lines[5]);
return {
id: +id,
items: items.split(", ").map(Number),
divisor: +divisor,
operation: new Function("old", `return (\n${operation}\n)`),
target: (value) => value % divisor === 0 ? +left : +right
};
}
Insert cell
Insert cell
[...simulateMonkeys(testMonkeys)]
Insert cell
function* simulateMonkeys(monkeys, rounds = 20) {
monkeys = copyMonkeys(monkeys);
monkeys.forEach((monkey) => monkey.inspections = 0);
for (let round = 1; round <= rounds; ++round) {
for (const monkey of monkeys) {
const items = monkey.items;
monkey.items = [];
monkey.inspections += items.length;
for (const item of items) {
const newItem = Math.floor(monkey.operation(item) / 3);
const targetId = monkey.target(newItem);
monkeys[targetId].items.push(newItem);
}
}
yield {round, monkeys: copyMonkeys(monkeys)};
}
}
Insert cell
function copyMonkeys(monkeys) {
return monkeys.map(copyMonkey);
}
Insert cell
function copyMonkey({items, ...monkey}) {
return {items: items.slice(), ...monkey};
}
Insert cell
testMonkeyInspections = {
for (const round of simulateMonkeys(testMonkeys)) {
yield round.monkeys.map((monkey) => monkey.inspections);
}
}
Insert cell
Insert cell
monkeys = parseMonkeys(input)
Insert cell
monkeyInspections = {
for (const round of simulateMonkeys(monkeys)) {
yield round.monkeys.map((monkey) => monkey.inspections);
}
}
Insert cell
evaluateMonkeyBusiness(monkeyInspections)
Insert cell
function evaluateMonkeyBusiness(inspections) {
const [a, b] = d3.sort(inspections).slice(-2);
return a * b;
}
Insert cell
function* simulateMonkeys2(monkeys, rounds) {
const divisor = monkeys.reduce((d, monkey) => d * monkey.divisor, 1);
monkeys = copyMonkeys(monkeys);
monkeys.forEach((monkey) => monkey.inspections = 0);
for (let round = 1; round <= rounds; ++round) {
for (const monkey of monkeys) {
const items = monkey.items;
monkey.items = [];
monkey.inspections += items.length;
for (const item of items) {
const newItem = monkey.operation(item) % divisor;
const targetId = monkey.target(newItem);
monkeys[targetId].items.push(newItem);
}
}
yield {round, monkeys: copyMonkeys(monkeys)};
}
}
Insert cell
testMonkeyInspections2 = {
for (const round of simulateMonkeys2(testMonkeys, 10000)) {
if (round.round % 100 === 0) {
yield round.monkeys.map((monkey) => monkey.inspections);
}
}
}
Insert cell
evaluateMonkeyBusiness(testMonkeyInspections2)
Insert cell
monkeyInspections2 = {
for (const round of simulateMonkeys2(monkeys, 10000)) {
if (round.round % 100 === 0) {
yield round.monkeys.map((monkey) => monkey.inspections);
}
}
}
Insert cell
evaluateMonkeyBusiness(monkeyInspections2)
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