Function aoc::y15d02::y15d02

source ·
pub fn y15d02(input: &str, part: u32) -> u32
Expand description

The solution for the day two challenge.

Given the input as a string we start by parsing all of the dimensions on each line. Then if we’re doing part 1 we calculate the area of each side, add them to our heap and then pop the smallest value back. The actual calculation comes directly from the challenge prompt: 2*l*w + 2*w*h + 2*h*l plus the extra (area of the smallest side that we just got).

If we’re doing part 2 then we calculate the cubic volume (l * w * h), and then find the smallest two sides by adding the dimensions to the heap and then popping the smallest two values back. The calculation again comes directly from the prompt and is equal to the diameter of the smallest side (two times the smallest two dimensions each) plus the volume previously calculated.

§Example

let input = "1x2x3\n4x5x6\n"; // probably read this from the input file...
assert_eq!(y15d02(input, 1), 192);
assert_eq!(y15d02(input, 2), 150);

§Min-Heap values

The rust documentation isn’t exactly clear about how to work with a min-heap which uses the same data structure as the max heap, but this helpful answer on stack overflow provides the answer, reproduced here:

let reversed = Reverse(42);
assert_eq!(reversed.0, 42);
// alternatively, using destructuring:
let Reverse(n) = reversed;
assert_eq!(n, 42);