advent_of_code/2023/1/1.rs

32 lines
947 B
Rust
Raw Permalink Normal View History

2023-12-02 13:51:49 +01:00
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::<u32>().unwrap();
}
println!("Total lines are: {}", accum);
}