pub fn y22d11(input: &str, rounds: u32, static_relief: bool) -> u64
Expand description
The solution for the day eleven challenge.
There are three input arguments: the input (monkey definitions) as a
string, the number of rounds to simulate as an integer, and a boolean
whether or not we should implement the “static relief” method (i.e., divide
the worry by 3
on each inspection). If it’s false
then we do part two
of the challenge where we reduce the worry using the modulo method.
We start by parsing the input. Each monkey’s definition is the same: it
takes six lines (seven including a trailing newline, so we split the input
into chunks of seven lines to parse it). The first line is the monkey’s
“name” or index, starting with 0
. The next line is a comma-separated
list of the items/worry that a monkey is currently holding. The next line
is the definition of how the worry changes each time the monkey inspects
an item. There are four possibilities: the monkey can add or multiply by
a static number or they monkey can add the item’s value to itself or
multiply the item’s value by itself. Finally, are three lines that tell
the monkey where to send the item next. The first of these three lines is
the number to divide the item by and if the result is 0
(i.e., no
remainder) then the monkey gives the item to the monkey on the second line
but otherwise (i.e., there is a remainder) then the monkey gives the
item to the monkey on the third line.
Parsing this input essentially amounts to splitting a bunch of whitespace
and parsing the integers found in certain indexes of the result. One
important implementation note is that the monkeys inspect the items in a
FIFO (first-in-first-out) order meaning that a standard vector is
unsuitable for tracking the items that the monkey is holding. We instead
use a std::collections::VecDeque
and its push_back()
and
pop_front()
methods to achieve this behavior.
Then we start simulating the rounds. We check each item that the monkey
is holding (pop()
ping them from the items list) and apply the worry
modification to it. We increase the number of inspections that the monkey
has done and then apply a worry relief strategy. We then check if the
item is divisible by the monkey’s test
number and then pass the item to
the corresponding monkey.
Finally, we sort the monkeys by how many inspections they did and return the number of inspections made by the top two monkeys multiplied together.
§Example
// probably read this from the input file...
let input = concat!(
"Monkey 0:\n",
" Starting items: 1, 2\n",
" Operation: new = old * 8\n",
" Test: divisible by 3\n",
" If true: throw to monkey 2\n",
" If false: throw to monkey 1\n",
"\n",
"Monkey 1:\n",
" Starting items: 3, 4, 5, 6\n",
" Operation: new = old + 9\n",
" Test: divisible by 5\n",
" If true: throw to monkey 0\n",
" If false: throw to monkey 2\n",
"\n",
"Monkey 2:\n",
" Starting items: 7\n",
" Operation: new = old * old\n",
" Test: divisible by 7\n",
" If true: throw to monkey 1\n",
" If false: throw to monkey 0",
);
assert_eq!(y22d11(&input, 5, true), 858);
assert_eq!(y22d11(&input, 5, false), 896);