/// Depth-first searches a digit to assign to `puzzle.lhs_needs[depth][i]`
/// and verifies that the resulting partial solution works.
/// The verification process might produce extra value bindings as a consequence of matching against RHS.
Depth-first searches a digit to assign to `puzzle.lhs_needs[depth][i]`
and verifies that the resulting partial solution works.
The verification process might produce extra value bindings as a consequence of matching against RHS.
Depth - first searches a digit to assign to ` puzzle . lhs_needs [ depth ] [ i ] ` and verifies that the resulting partial solution works . The verification process might produce extra value bindings as a consequence of matching against RHS .
fn search(puzzle: &Puzzle, sol: &mut PartialSolution, depth: usize, i: usize) -> Option<PartialSolution>{
    if depth == puzzle.lhs_needs.len() {
        let sol_v = verify(puzzle, sol, None)?;
        return Some(sol_v)
    }
    let cur_needs = &puzzle.lhs_needs[depth];
    if i >= cur_needs.len() {
        let mut sol_v = verify(puzzle, sol, Some(depth))?;
        return search(puzzle, &mut sol_v, depth + 1, 0);
    }
    let cur_char = cur_needs[i];
    if sol.assigns.contains_key(&cur_char) {
        return search(puzzle, sol, depth, i + 1);
    }
    let non_zero = puzzle.non_zeros.contains(&cur_char);
    let alts: Vec<u8> = 
        sol.unassigned_digits.iter().filter(|i| {!non_zero || **i != 0}).copied().collect();
    for val in alts {
        sol.unassigned_digits.remove(&val);
        sol.assigns.insert(cur_char, val);
        if let Some(r) = search(puzzle, sol, depth, i +1) {
            return Some(r)
        }
        sol.assigns.remove(&cur_char);
        sol.unassigned_digits.insert(val);
    }
    None
}
fn search ( puzzle : & Puzzle , sol : & mut PartialSolution , depth : usize , i : usize ) -> Option < PartialSolution > { if depth == puzzle . lhs_needs . len ( ) { let sol_v = verify ( puzzle , sol , None ) ? ; return Some ( sol_v ) } let cur_needs = & puzzle . lhs_needs [ depth ] ; if i >= cur_needs . len ( ) { let mut sol_v = verify ( puzzle , sol , Some ( depth ) ) ? ; return search ( puzzle , & mut sol_v , depth + 1 , 0 ) ; } let cur_char = cur_needs [ i ] ; if sol . assigns . contains_key ( & cur_char ) { return search ( puzzle , sol , depth , i + 1 ) ; } let non_zero = puzzle . non_zeros . contains ( & cur_char ) ; let alts : Vec < u8 > = sol . unassigned_digits . iter ( ) . filter ( | i | { ! non_zero || * * i != 0 } ) . copied ( ) . collect ( ) ; for val in alts { sol . unassigned_digits . remove ( & val ) ; sol . assigns . insert ( cur_char , val ) ; if let Some ( r ) = search ( puzzle , sol , depth , i + 1 ) { return Some ( r ) } sol . assigns . remove ( & cur_char ) ; sol . unassigned_digits . insert ( val ) ; } None }
Depth-first searches a digit to assign to `puzzle.lhs_needs[depth][i]`
and verifies that the resulting partial solution works.
Depth - first searches a digit to assign to ` puzzle . lhs_needs [ depth ] [ i ] ` and verifies that the resulting partial solution works .
puzzle
&Puzzle
sol
&mut PartialSolution
depth
usize
i
usize