advent_of_code_2024::day_22

Function shift_diff_into_sequence_id

source
fn shift_diff_into_sequence_id(state: &mut usize, prev: usize, current: usize)
Expand description

For performance reasons the sequence of four diffs before the current price is packed into a 20-bit integer

Each diff is max 19, so fits in 5 bits. Given the first four diffs packed like so:

    11111222223333344444
&   00000222223333344444 : The first diff is removed by applying a bit mask
<<5 22222333334444400000 : The previous numbers are left shifted
+   22222333334444455555 : The new diff is added, taking the least-significant 5 bits