Solution to the 5th problem
Problem A
The answer for part A is pretty straightforward, as it simply requires that we execute each movement, the amount of times defined in the amount field of each move. Each stack work as a stack data structure, therefore the last element added is the first one to be removed. This logic is executed until there are no movements left.
» cargo build -p aoc-22 --release && hyperfine ./target/release/aoc-22 -N --warmup 5
Benchmark 1: ./target/release/aoc-22
Time (mean ± σ): 4.8 ms ± 1.7 ms [User: 1.1 ms, System: 0.9 ms]
Range (min … max): 3.1 ms … 18.5 ms 511 runs
Problem B
Part B is really similar to the part A, yet in this case when
we remove elements from a stack, we cannot do it using a FIFO
approach, but rather we need to move the amount of items at the
same time, keeping their order. The most simple solution is to slice
method from the array, but starting from the
end, and taking amount
elements. This way, we keep
the order of the elements without needing to reverse them.
If you check the solver code, you'll see that we are using splice
instead, so we can mutate the array from
which we are extracting the elements, at the same time we are
taking them. If we were to use slice
, we would
then have to manually mutate the array, removing the extracted
elements.
» cargo build -p aoc-23 --release && hyperfine ./target/release/aoc-23 -N --warmup 5
Benchmark 1: ./target/release/aoc-22
Time (mean ± σ): 5.8 ms ± 4.0 ms [User: 1.2 ms, System: 1.0 ms]
Range (min … max): 3.2 ms … 55.5 ms 421 runs
Customize your input
Keep in mind that for this problem, proper spacing of this column is required, as otherwise the parsing won't work as expected.
Initial stacks
Movements
Visualization
Configuration
Stacks
No input defined yet!