use std::fs;

fn extract_first_match(line: &str) -> i32 {
    for i in 0..line.len(){
        let slice = &line[i..line.len()];
        if let Some(ch) =slice.chars().next() {
            if ch.is_digit(10) {
                return ch.to_digit(10).unwrap() as i32;
            }
        }
    }
    0
}

fn extract_last_match(line: &str) -> i32 {
    for i in (0..line.len()).rev() {
        let slice = &line[i..line.len()];
        if let Some(ch) =slice.chars().next() {
            if ch.is_digit(10) {
                return ch.to_digit(10).unwrap() as i32;
            }
        }
    }
    0
}


fn main() {
    let input = fs::read_to_string("input_day1.txt").unwrap();
    let lines = input.lines();
    let mut total = 0;
    for line in lines {
        let first = extract_first_match(line);
        let last = extract_last_match(line);
        total += first * 10 + last;
    }
    println!("{}", total);
}
