pub fn power_consumption(path: &str) -> u32 {
    let diagnostic_report = parse(path);
    let limit = diagnostic_report[0].len();
    let report_length = diagnostic_report.len();

    let mut positions = HashMap::<u32, u32>::new();

    for report in diagnostic_report {
        for (i, c) in report.chars().enumerate() {
            if String::from(c) == "1" {
                *positions.entry(i.try_into().unwrap()).or_insert(0) += 1;
            }
        }
    }

    let mut gamma_rate = String::new();
    let mut epsilon_rate = String::new();
    let bit_size = (report_length / 2).try_into().unwrap();

    for i in 0..limit {
        let position = positions.get(&i.try_into().unwrap());

        if position > Some(&bit_size) {
            gamma_rate.push_str("1");
            epsilon_rate.push_str("0");
        } else {
            gamma_rate.push_str("0");
            epsilon_rate.push_str("1");
        }
    }

    return to_decimal(&gamma_rate) * to_decimal(&epsilon_rate);
}
// Part one: Find the power consumption of a submarine
Part one: Find the power consumption of a submarine
Part one : Find the power consumption of a submarine
pub fn power_consumption(path: &str) -> u32 {
    let diagnostic_report = parse(path);
    let limit = diagnostic_report[0].len();
    let report_length = diagnostic_report.len();
    let mut positions = HashMap::<u32, u32>::new();
    for report in diagnostic_report {
        for (i, c) in report.chars().enumerate() {
            if String::from(c) == "1" {
                *positions.entry(i.try_into().unwrap()).or_insert(0) += 1;
            }
        }
    }
    let mut gamma_rate = String::new();
    let mut epsilon_rate = String::new();
    let bit_size = (report_length / 2).try_into().unwrap();
    for i in 0..limit {
        let position = positions.get(&i.try_into().unwrap());
        if position > Some(&bit_size) {
            gamma_rate.push_str("1");
            epsilon_rate.push_str("0");
        } else {
            gamma_rate.push_str("0");
            epsilon_rate.push_str("1");
        }
    }
    return to_decimal(&gamma_rate) * to_decimal(&epsilon_rate);
}
pub fn power_consumption ( path : & str ) -> u32 { let diagnostic_report = parse ( path ) ; let limit = diagnostic_report [ 0 ] . len ( ) ; let report_length = diagnostic_report . len ( ) ; let mut positions = HashMap :: < u32 , u32 > :: new ( ) ; for report in diagnostic_report { for ( i , c ) in report . chars ( ) . enumerate ( ) { if String :: from ( c ) == " " { * positions . entry ( i . try_into ( ) . unwrap ( ) ) . or_insert ( 0 ) += 1 ; } } } let mut gamma_rate = String :: new ( ) ; let mut epsilon_rate = String :: new ( ) ; let bit_size = ( report_length / 2 ) . try_into ( ) . unwrap ( ) ; for i in 0 .. limit { let position = positions . get ( & i . try_into ( ) . unwrap ( ) ) ; if position > Some ( & bit_size ) { gamma_rate . push_str ( " " ) ; epsilon_rate . push_str ( " " ) ; } else { gamma_rate . push_str ( " " ) ; epsilon_rate . push_str ( " " ) ; } } return to_decimal ( & gamma_rate ) * to_decimal ( & epsilon_rate ) ; }
Part one: Find the power consumption of a submarine
Part one : Find the power consumption of a submarine
path
&str