// tag::solve_with_while[]
/// Return the product on the combination of `n` numbers in list that sum up to 2020.
///
/// The solution uses a while loop which updates indices for all elements of the combination.
///
/// # Examples
///
/// ```
/// let list: Vec<i32> = vec![1721, 979, 366, 299, 675, 1456];
/// let prod2 = mr_kaffee_2020_01::solve_with_while(&list, 2);
/// assert_eq!(prod2, 514_579);
/// let prod3 = mr_kaffee_2020_01::solve_with_while(&list, 3);
/// assert_eq!(prod3, 241_861_950);
/// ```
:solve_with_while[]
Return the product on the combination of `n` numbers in list that sum up to 2020.

The solution uses a while loop which updates indices for all elements of the combination.
: solve_with_while [] Return the product on the combination of ` n ` numbers in list that sum up to 2020 . The solution uses a while loop which updates indices for all elements of the combination .
pub fn solve_with_while(list: &[i32], n: usize) -> i32 {
    let mut indices: Vec<usize> = (0..n).collect();
    while indices[n - 1] <= list.len() {
        // if sum matches, return product
        if indices.iter().fold(0, |sum, idx| sum + list[*idx]) == 2020 {
            return indices.iter().fold(1, |prod, idx| prod * list[*idx]);
        }
        // update indices
        let mut i = 0;
        // find first index which can be incremented
        while i < indices.len() - 1 && indices[i] == indices[i + 1] - 1 {
            i += 1;
        }
        // increment index
        indices[i] += 1;
        // reset all lower indices
        for j in 0..i {
            indices[j] = j;
        }
    }
    panic!("Nothing found.");
}
pub fn solve_with_while ( list : & [ i32 ] , n : usize ) -> i32 { let mut indices : Vec < usize > = ( 0 .. n ) . collect ( ) ; while indices [ n - 1 ] <= list . len ( ) { // if sum matches, return product if indices . iter ( ) . fold ( 0 , | sum , idx | sum + list [ * idx ] ) == 2020 { return indices . iter ( ) . fold ( 1 , | prod , idx | prod * list [ * idx ] ) ; } // update indices let mut i = 0 ; // find first index which can be incremented while i < indices . len ( ) - 1 && indices [ i ] == indices [ i + 1 ] - 1 { i += 1 ; } // increment index indices [ i ] += 1 ; // reset all lower indices for j in 0 .. i { indices [ j ] = j ; } } panic ! ( " " ) ; }
tag::solve_with_while[]
Return the product on the combination of `n` numbers in list that sum up to 2020.
tag :: solve_with_while [] Return the product on the combination of ` n ` numbers in list that sum up to 2020 .
list
&[i32]
n
usize
examples
None