Function advent_of_code_2021::day_3::analyse_life_support [−][src]
Expand description
This solves part two, returning the pair of required values. The solution calls for successively filtering the input array until only one value array, which works well as a recursive function. The base case for this is when the provided array is of length 1, and returns the remaining value if it is present. Otherwise split the input based on whether the bit we care about is set, then recurse the largest/smallest half based on which value is being calculated, and the bit position advanced by 1. Unlike part one, the recursion means there isn’t a neat trick to invert the first result to product the second, so the recursive function is called twice, with a flag used to switch the mode.
The recursive function will panic! if it has an empty data array or the position is negative.
e.g an input of vec![0b100, 0b101, 0b110] would panic when calculating the CO2 scrubber rating
because all values share the same first bit.
Example from puzzle specification
let test_data = vec![
0b00100,
0b11110,
0b10110,
0b10111,
0b10101,
0b01111,
0b00111,
0b11100,
0b10000,
0b11001,
0b00010,
0b01010,
];
assert_eq!(analyse_life_support(&test_data(), 5), (23, 10));