Public
Edited
Dec 19, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
reliefFunctionPart1 = worry => Math.floor(worry / 3)
Insert cell
Insert cell
solvePart1 = R.pipe(
parseMonkeys,
monkeys => simulate(simulateRound(inspect(reliefFunctionPart1)), monkeys, R.range(1, 21), {discardHistory: true}),
R.last,
calculateMonkeyBusiness
)
Insert cell
solvePart1(input)
Insert cell
Insert cell
Insert cell
reliefFunctionPart2 = (monkeys) => {
const divisorProduct = monkeys.reduce((combined, monkey) => combined * monkey.targeting[0], 1)

return worry => worry % divisorProduct
}
Insert cell
solvePart2 = R.pipe(
parseMonkeys,
monkeys => simulate(simulateRound(inspect(reliefFunctionPart2(monkeys))), monkeys, R.range(1, 10001), {discardHistory: true}),
R.last,
calculateMonkeyBusiness
)
Insert cell
solvePart2(input)
Insert cell
Insert cell
encodeItem = R.curry((monkeys, item) => new Map(monkeys.map(({targeting: [divisor]}) => [divisor, item % divisor])))
Insert cell
encodeMonkeys = monkeys => monkeys.map(monkey => ({...monkey, items: monkey.items.map(item => encodeItem(monkeys, item))}))
Insert cell
adjustWorryLevelPart2 = ([operation, argument], encodedItem) => {
if (operation === '*' && argument === 'old') return new Map([...encodedItem.entries()].map(([divisor, x]) => {
return [divisor, (x * x) % divisor]
}))

if (argument === 'old') throw new Error('old value is unsupported for computation')

const y = parseInt(argument)

const updatedItem = new Map([...encodedItem.entries()].map(([divisor, x]) => {
return [divisor, (operation === '*' ? x * y : x + y) % divisor]
}))

return updatedItem
}
Insert cell
inspectPart2 = (monkey, encodedItem) => {
const updatedItem = adjustWorryLevelPart2(monkey.inspection, encodedItem)

const [divisor, targetTrue, targetFalse] = monkey.targeting

const target = updatedItem.get(divisor) === 0 ? targetTrue : targetFalse

return [updatedItem, target]
}
Insert cell
solvePart2Complicated = R.pipe(
parseMonkeys,
encodeMonkeys,
monkeys => simulate(simulateRound(inspectPart2), monkeys, R.range(1, 10001), {discardHistory: true}),
R.last,
calculateMonkeyBusiness
)
Insert cell
solvePart2Complicated(input)
Insert cell
Insert cell
monkeyRegex = /Monkey (?<id>\d):\s+Starting items: (?<items>[\d, ]+)\n\s+Operation: new = old (?<operation>.) (?<argument>.+)\s+Test: divisible by (?<divisor>\d+)\s+If true: throw to monkey (?<targetTrue>\d)\s+If false: throw to monkey (?<targetFalse>\d)/gm
Insert cell
parseMonkeys = text => [...text.matchAll(monkeyRegex)].map(match => {
const {id, items, operation, argument, divisor, targetTrue, targetFalse} = match.groups

return {
id,
items: items.split(/,\s+/).map(x => parseInt(x)),
inspection: [operation, argument],
targeting: [divisor, targetTrue, targetFalse],
actionCount: 0
}
})
Insert cell
adjustWorryLevel = ([operation, argument], x) => {
const y = argument === 'old' ? x : parseInt(argument)

if (operation === '+') return x + y
if (operation === '*') return x * y
}
Insert cell
inspect = reliefFunction => (monkey, worry) => {
worry = adjustWorryLevel(monkey.inspection, worry)

worry = reliefFunction(worry)

const [divisor, targetTrue, targetFalse] = monkey.targeting
const target = !(worry % divisor) ? targetTrue : targetFalse

return [worry, target]
}
Insert cell
Insert cell
simulateRound = (inspectionStrategy) => (round, monkeys) => {
monkeys = R.clone(monkeys)

for (const monkey of monkeys) {
for (const item of monkey.items) {
const [adjustedItem, target] = inspectionStrategy(monkey, item)

monkeys.find(x => x.id === target).items.push(adjustedItem)
monkey.actionCount++
}
monkey.items = []
}

return monkeys
}
Insert cell
calculateMonkeyBusiness = monkeys => [...monkeys]
.sort((a, b) => b.actionCount - a.actionCount)
.slice(0, 2)
.reduce((total, monkey) => total * monkey.actionCount, 1)
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