fn careful_analysis(digits: &SegDisplay, verbose: bool) -> [usize; 7] {
    assert_eq!(digits.0.len(), 10);
    // The segment numbering is shown on the left.
    // 0  ---      The mapping of segment number to byte position in the u64 is below.
    // 1 |   | 2    ___ ___ ___ ___ ___ ___ ___ ___ ___
    // 3  ---      |_x_|_7_|_6_|_5_|_4_|_3_|_2_|_1_|_0_|
    // 4 |   | 5
    // 6  ---
    let mut seg_codec = [0; 7];
    // Generate counts of number of digits in which each segment appears.
    // Each segment is conveniently laid over disjoint bytes of a u64,
    // so simply summing segments gives the right number.
    // We won't have more than 7 segments so we can't overflow the bytes.
    let counts: u64 = digits.0.iter().map(|digit| digit.0).sum();
    // Now we have assignments for segment 1, 4, and 5, which uniquely appear 6, 4, and 9 times.
    {
        let mut counts = counts;
        for index in 0..7 {
            match counts & 0xff {
                6 => {
                    seg_codec[index] = 1;
                    if verbose {
                        println!(
                           "  segment {} has {} occurrences, so it must be segment {}",
                           Segment::from_id(index).to_string(), 6, 1,
                       );
                    }
                }, // segment 1 appears in 0,4,5,6,8,9
                4 =>  {
                    seg_codec[index] = 4; // segment 4 appears in 0,2,6,8
                    if verbose {
                        println!(
                           "  segment {} has {} occurrences, so it must be segment {}",
                           Segment::from_id(index).to_string(), 4, 4,
                       );
                    }
                },
                9 => {
                    seg_codec[index] = 5; // segment 5 appears in 0,1,3,4,5,6,7,8,9
                    if verbose {
                        println!(
                           "  segment {} has {} occurrences, so it must be segment {}",
                           Segment::from_id(index).to_string(), 9, 5,
                       );
                    }
                },
                _ => (),
            }
            counts >>= 8;
        }
    }
    // Now identify the numbers we know to be 1 (2 segs), 4 (4 segs), 7 (3 segs), or 8 (7 segs).
    // While we're at it, repeat the segment counts without these numbers.
    // This identifies segments 2 and 3, which have counts 4 and 5.
    let mut digit_seven = 0;
    let counts: u64 = digits.0.iter().filter_map(|digit| {
        match digit.0.count_ones() {
            2 => {
                if verbose {
                    println!(
                        "  digit {} has {} segments, so must be digit {}",
                        digit, 2, 1,
                    );
                }
                None
            },
            4 => {
                if verbose {
                    println!(
                        "  digit {} has {} segments, so must be digit {}",
                        digit, 4, 4,
                    );
                }
                None
            },
            3 => {
                if verbose {
                    println!(
                        "  digit {} has {} segments, so must be digit {}",
                        digit, 3, 7,
                    );
                }
                digit_seven = digit.0;
                None
            },
            7 => {
                if verbose {
                    println!(
                        "  digit {} has {} segments, so must be digit {}",
                        digit, 7, 8,
                    );
                }
                None
            },
            _ => Some(digit.0),
        }
    }).sum();
    // Now we know the assignments for segments 2 and 3, which have counts 4 and 5.
    {
        let mut counts = counts;
        for index in 0..7 {
            // Skip segments we already identified (1, 4, 5)
            if seg_codec[index] == 0 {
                match counts & 0xff {
                    4 => {
                        seg_codec[index] = 2; // segment 2 appears in 0,2,3,9 (ignoring 1,4,7,8)
                        if verbose {
                            println!(
                               "  segment {} has {} occurrences, so it must be segment {}",
                               Segment::from_id(index).to_string(), 4, 2,
                           );
                        }
                    },
                    5 => {
                        seg_codec[index] = 3; // segment 3 appears in 2,3,5,6,9 (ignoring 1,4,7,8)
                        if verbose {
                            println!(
                               "  segment {} has {} occurrences, so it must be segment {}",
                               Segment::from_id(index).to_string(), 5, 3,
                           );
                        }
                    },
                    _ => (),
                }
            }
            counts >>= 8;
        }
    }
    // Only segments 0 and 6 remain unassigned.
    // Segment 0 appears in digit 7 along with 2 and 5, which we've already decoded.
    // The unassigned segment in this position must be segment 0, and the last remaining
    // unassigned segment must be segment 6.
    assert_ne!(digit_seven, 0);
    for (index, decoded_seg) in seg_codec.iter().enumerate() {
        let shift = index * 8;
        if verbose {
            let seg = Segment::from_id(index);
            println!("  testing segment {} ({}, {:056b}) (known to be {}) against digit 7 ({:056b})",
                seg, index, seg.0, *decoded_seg, digit_seven);
            println!("  shift and segment are {}, {}", shift, (digit_seven >> shift) & 0xff);
        }
        if *decoded_seg == 0 && ((digit_seven >> shift) & 0xff) == 0 {
            seg_codec[index] = 6;
            if verbose {
                println!(
                   "  unidentified segment {} does not belong to digit 7 ({:056b}) \
                   , so it must be segment {}",
                   Segment::from_id(index).to_string(), digit_seven, 6,
               );
            }
            break;
        }
        // Don't need to explicitly assign 0 in the codec, because all slots are 0 by default.
        // But we are done once we've found the slot for 6.
    }
    if verbose {
        println!("  final segment map: {:?}", seg_codec);
    }
    // Done!
    seg_codec
}
// after some careful analysis, the mapping between signal wires and segments makes sense :-)
//
// Returns a segment decoder D such that D[s] is the segment for the encoded segment s.
after some careful analysis, the mapping between signal wires and segments makes sense :-)
Returns a segment decoder D such that D[s] is the segment for the encoded segment s.
after some careful analysis the mapping between signal wires and segments makes sense : - ) Returns a segment decoder D such that D [ s ] is the segment for the encoded segment s .
fn careful_analysis(digits: &SegDisplay, verbose: bool) -> [usize; 7] {
    assert_eq!(digits.0.len(), 10);
    // The segment numbering is shown on the left.
    // 0  ---      The mapping of segment number to byte position in the u64 is below.
    // 1 |   | 2    ___ ___ ___ ___ ___ ___ ___ ___ ___
    // 3  ---      |_x_|_7_|_6_|_5_|_4_|_3_|_2_|_1_|_0_|
    // 4 |   | 5
    // 6  ---
    let mut seg_codec = [0; 7];
    // Generate counts of number of digits in which each segment appears.
    // Each segment is conveniently laid over disjoint bytes of a u64,
    // so simply summing segments gives the right number.
    // We won't have more than 7 segments so we can't overflow the bytes.
    let counts: u64 = digits.0.iter().map(|digit| digit.0).sum();
    // Now we have assignments for segment 1, 4, and 5, which uniquely appear 6, 4, and 9 times.
    {
        let mut counts = counts;
        for index in 0..7 {
            match counts & 0xff {
                6 => {
                    seg_codec[index] = 1;
                    if verbose {
                        println!(
                           "  segment {} has {} occurrences, so it must be segment {}",
                           Segment::from_id(index).to_string(), 6, 1,
                       );
                    }
                }, // segment 1 appears in 0,4,5,6,8,9
                4 =>  {
                    seg_codec[index] = 4; // segment 4 appears in 0,2,6,8
                    if verbose {
                        println!(
                           "  segment {} has {} occurrences, so it must be segment {}",
                           Segment::from_id(index).to_string(), 4, 4,
                       );
                    }
                },
                9 => {
                    seg_codec[index] = 5; // segment 5 appears in 0,1,3,4,5,6,7,8,9
                    if verbose {
                        println!(
                           "  segment {} has {} occurrences, so it must be segment {}",
                           Segment::from_id(index).to_string(), 9, 5,
                       );
                    }
                },
                _ => (),
            }
            counts >>= 8;
        }
    }
    // Now identify the numbers we know to be 1 (2 segs), 4 (4 segs), 7 (3 segs), or 8 (7 segs).
    // While we're at it, repeat the segment counts without these numbers.
    // This identifies segments 2 and 3, which have counts 4 and 5.
    let mut digit_seven = 0;
    let counts: u64 = digits.0.iter().filter_map(|digit| {
        match digit.0.count_ones() {
            2 => {
                if verbose {
                    println!(
                        "  digit {} has {} segments, so must be digit {}",
                        digit, 2, 1,
                    );
                }
                None
            },
            4 => {
                if verbose {
                    println!(
                        "  digit {} has {} segments, so must be digit {}",
                        digit, 4, 4,
                    );
                }
                None
            },
            3 => {
                if verbose {
                    println!(
                        "  digit {} has {} segments, so must be digit {}",
                        digit, 3, 7,
                    );
                }
                digit_seven = digit.0;
                None
            },
            7 => {
                if verbose {
                    println!(
                        "  digit {} has {} segments, so must be digit {}",
                        digit, 7, 8,
                    );
                }
                None
            },
            _ => Some(digit.0),
        }
    }).sum();
    // Now we know the assignments for segments 2 and 3, which have counts 4 and 5.
    {
        let mut counts = counts;
        for index in 0..7 {
            // Skip segments we already identified (1, 4, 5)
            if seg_codec[index] == 0 {
                match counts & 0xff {
                    4 => {
                        seg_codec[index] = 2; // segment 2 appears in 0,2,3,9 (ignoring 1,4,7,8)
                        if verbose {
                            println!(
                               "  segment {} has {} occurrences, so it must be segment {}",
                               Segment::from_id(index).to_string(), 4, 2,
                           );
                        }
                    },
                    5 => {
                        seg_codec[index] = 3; // segment 3 appears in 2,3,5,6,9 (ignoring 1,4,7,8)
                        if verbose {
                            println!(
                               "  segment {} has {} occurrences, so it must be segment {}",
                               Segment::from_id(index).to_string(), 5, 3,
                           );
                        }
                    },
                    _ => (),
                }
            }
            counts >>= 8;
        }
    }
    // Only segments 0 and 6 remain unassigned.
    // Segment 0 appears in digit 7 along with 2 and 5, which we've already decoded.
    // The unassigned segment in this position must be segment 0, and the last remaining
    // unassigned segment must be segment 6.
    assert_ne!(digit_seven, 0);
    for (index, decoded_seg) in seg_codec.iter().enumerate() {
        let shift = index * 8;
        if verbose {
            let seg = Segment::from_id(index);
            println!("  testing segment {} ({}, {:056b}) (known to be {}) against digit 7 ({:056b})",
                seg, index, seg.0, *decoded_seg, digit_seven);
            println!("  shift and segment are {}, {}", shift, (digit_seven >> shift) & 0xff);
        }
        if *decoded_seg == 0 && ((digit_seven >> shift) & 0xff) == 0 {
            seg_codec[index] = 6;
            if verbose {
                println!(
                   "  unidentified segment {} does not belong to digit 7 ({:056b}) \
                   , so it must be segment {}",
                   Segment::from_id(index).to_string(), digit_seven, 6,
               );
            }
            break;
        }
        // Don't need to explicitly assign 0 in the codec, because all slots are 0 by default.
        // But we are done once we've found the slot for 6.
    }
    if verbose {
        println!("  final segment map: {:?}", seg_codec);
    }
    // Done!
    seg_codec
}
fn careful_analysis ( digits : & SegDisplay , verbose : bool ) -> [ usize ; 7 ] { assert_eq ! ( digits 0 len ( ) 10 ) ; // The segment numbering is shown on the left. // 0  ---      The mapping of segment number to byte position in the u64 is below. // 1 |   | 2    ___ ___ ___ ___ ___ ___ ___ ___ ___ // 3  ---      |_x_|_7_|_6_|_5_|_4_|_3_|_2_|_1_|_0_| // 4 |   | 5 // 6  --- let mut seg_codec = [ 0 ; 7 ] ; // Generate counts of number of digits in which each segment appears. // Each segment is conveniently laid over disjoint bytes of a u64, // so simply summing segments gives the right number. // We won't have more than 7 segments so we can't overflow the bytes. let counts : u64 = digits . 0 . iter ( ) . map ( | digit | digit . 0 ) . sum ( ) ; // Now we have assignments for segment 1, 4, and 5, which uniquely appear 6, 4, and 9 times. { let mut counts = counts ; for index in 0 .. 7 { match counts & 0xff { 6 => { seg_codec [ index ] = 1 ; if verbose { println ! ( " " Segment from_id ( index ) to_string ( ) 6 1 ) ; } } , // segment 1 appears in 0,4,5,6,8,9 4 => { seg_codec [ index ] = 4 ; // segment 4 appears in 0,2,6,8 if verbose { println ! ( " " Segment from_id ( index ) to_string ( ) 4 4 ) ; } } , 9 => { seg_codec [ index ] = 5 ; // segment 5 appears in 0,1,3,4,5,6,7,8,9 if verbose { println ! ( " " Segment from_id ( index ) to_string ( ) 9 5 ) ; } } , _ => ( ) , } counts >>= 8 ; } } // Now identify the numbers we know to be 1 (2 segs), 4 (4 segs), 7 (3 segs), or 8 (7 segs). // While we're at it, repeat the segment counts without these numbers. // This identifies segments 2 and 3, which have counts 4 and 5. let mut digit_seven = 0 ; let counts : u64 = digits . 0 . iter ( ) . filter_map ( | digit | { match digit . 0 . count_ones ( ) { 2 => { if verbose { println ! ( " " digit 2 1 ) ; } None } , 4 => { if verbose { println ! ( " " digit 4 4 ) ; } None } , 3 => { if verbose { println ! ( " " digit 3 7 ) ; } digit_seven = digit . 0 ; None } , 7 => { if verbose { println ! ( " " digit 7 8 ) ; } None } , _ => Some ( digit . 0 ) , } } ) . sum ( ) ; // Now we know the assignments for segments 2 and 3, which have counts 4 and 5. { let mut counts = counts ; for index in 0 .. 7 { // Skip segments we already identified (1, 4, 5) if seg_codec [ index ] == 0 { match counts & 0xff { 4 => { seg_codec [ index ] = 2 ; // segment 2 appears in 0,2,3,9 (ignoring 1,4,7,8) if verbose { println ! ( " " Segment from_id ( index ) to_string ( ) 4 2 ) ; } } , 5 => { seg_codec [ index ] = 3 ; // segment 3 appears in 2,3,5,6,9 (ignoring 1,4,7,8) if verbose { println ! ( " " Segment from_id ( index ) to_string ( ) 5 3 ) ; } } , _ => ( ) , } } counts >>= 8 ; } } // Only segments 0 and 6 remain unassigned. // Segment 0 appears in digit 7 along with 2 and 5, which we've already decoded. // The unassigned segment in this position must be segment 0, and the last remaining // unassigned segment must be segment 6. assert_ne ! ( digit_seven 0 ) ; for ( index , decoded_seg ) in seg_codec . iter ( ) . enumerate ( ) { let shift = index * 8 ; if verbose { let seg = Segment :: from_id ( index ) ; println ! ( " " seg index seg 0 decoded_seg digit_seven ) ; println ! ( " " shift ( digit_seven shift ) 0xff ) ; } if * decoded_seg == 0 && ( ( digit_seven >> shift ) & 0xff ) == 0 { seg_codec [ index ] = 6 ; if verbose { println ! ( " \
 " Segment from_id ( index ) to_string ( ) digit_seven 6 ) ; } break ; } // Don't need to explicitly assign 0 in the codec, because all slots are 0 by default. // But we are done once we've found the slot for 6. } if verbose { println ! ( " " seg_codec ) ; } // Done! seg_codec }
after some careful analysis, the mapping between signal wires and segments makes sense :-)
Returns a segment decoder D such that D[s] is the segment for the encoded segment s.
after some careful analysis the mapping between signal wires and segments makes sense : - ) Returns a segment decoder D such that D [ s ] is the segment for the encoded segment s .
digits
&SegDisplay
verbose
bool