Monkeys have your belongings and are throwing them to each other!
Each monkey follows a few rules that determine to which other monkey they throw an item.
The monkeys are numbered, and they take turns throwing all their items.
When a monkey throws items, it throws them in order, from oldest to newest.
A determining factor in those rules is how worried you are about an item (expressed as a number, the worry level).
Today’s input is a list of monkeys with the rules that each one follows.
An example input looks like this:
Monkey 0 starts with 2 items.
the first has worry level 79
the second has worry level 98
Whenever a monkey inspects an item, they apply their operation.
This operation changes an item’s worry level.
Monkey 0 multiplies the worry level of the inspected item by 19.
Before a monkey thows an item, it applies a test to the current worry level.
Monkey 0 checks if the worry level is divisible by 23.
if it is, it throws the item to monkey 2
if it is not, it throws the item to monkey 3
Parsing
I decided to parse the input into a list of Monkeys.
A Monkey directly maps to a monkey in the input.
An operation is always either a multiplication or an addition.
The first operand is always the old worry level.
The second operand is either the old worry level or a literal number.
Since the second one is the only one that ever changes, that’s the one I store.
The test has three parts, the divisibility check, which monkey to throw to if it succeeds, and which to throw to if it fails.
The parsing code is quite boring, I turn the input into blocks for each monkey.
Every block, I parse into a Monkey and add it to the monkeys list.
To do this I turn each string block into lines, and per line consumed, I extract the wanted info from it.
Part 1
After each monkey inspects an item but before it tests your worry level,
your relief that the monkey’s inspection didn’t damage the item causes your worry level to be divided by three and rounded down to the nearest integer.
You track the amount of times a monkey inspects any item.
The product of the 2 largest amounts is called the monkey business.
The question asks what the level of monkey business is after 20 rounds.
Starting skeleton code for part1:
Helper functions
To make write out those steps a bit clearer, I created a few helpers.
A method on Value gets a number for one whenever you give it the old worry level.
A method on Operation does a monkey’s operation.
It calculated the new worry level if you give it the old worry level.
Filling in the code from before (and catching the borrow checker issue):
It works, that’s part1 done!
Final code
Part 2
You are no longer relieved after a monkey inspects an item.
The question asks what the level of monkey business is after 10000 rounds.
Ah, this will be easy, change the 20 to 10000 and remove the / 3 part
Well, I was right, but there’s one very minor, serious problem of needing a supercomputer for this to finish in a reasonable timeframe.
The worry levels get huge.
Absolutely massive.
Gigantic.
I’m in awe of the size of these numbers.
I took a common multiple of all divisibility checks, and took the modulus of the new worry level with that common multiple before I throw the item.
This will not affect any future divisibility checks.
Why doesn’t it matter?:
The future checked item was %ed with a multiple of its divisibility check, this doesn’t affect the result of such a check.