advent_of_code/2023/1/1.rs
kleph 6e3705e763
All checks were successful
continuous-integration/drone/push Build is passing
2023 - Day 1 part 1 - rust
2023-12-02 13:53:38 +01:00

31 lines
947 B
Rust

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);
}