diff --git a/2023/1/1.rs b/2023/1/1.rs new file mode 100644 index 0000000..af437dd --- /dev/null +++ b/2023/1/1.rs @@ -0,0 +1,31 @@ +use std::io::{BufRead, BufReader}; +use std::fs::File; + +fn main (){ + // let filename = "./input_example.txt"; + let filename = "./input.txt"; + let file = BufReader::new(File::open(filename).expect("Unable to open file")); + let mut accum: u32 = 0; + + for line in file.lines() { + let mut first_num: char = 'z'; + let mut last_num: char = 'z'; + // println!("{}", line.unwrap()); + for c in line.unwrap().to_string().chars() { + if '0' <= c && c <= '9' { + if first_num == 'z' { + first_num = c; + } + last_num = c; + } + } + + let mut line_num = String::from(first_num); + line_num.push(last_num); + // println!("first: {}, last: {}", first_num, last_num); + // println!("line_num: {}", line_num); + accum += line_num.parse::().unwrap(); + } + + println!("Total lines are: {}", accum); +}