advent_of_code/2023/day1/rust/part_one.rs

34 lines
835 B
Rust
Executable File

use std::fs::File;
use std::io::{self, prelude::*, BufReader};
fn process_line(line: String) -> u32 {
let mut first_digit: u32 = 0;
let mut last_digit: u32 = 0;
for i in line.chars() {
if i.is_numeric() {
first_digit = i as u32 - '0' as u32;
break;
}
}
for i in line.chars().rev() {
if i.is_numeric() {
last_digit = i as u32 - '0' as u32;
break;
}
}
println!("{}{}", first_digit, last_digit);
return first_digit*10+last_digit;
}
fn main() -> io::Result<()> {
let mut sum: u32 = 0;
let file = File::open("../input.txt")?;
let reader = BufReader::new(file);
for line in reader.lines() {
sum = process_line(line?) + sum;
}
println!("{}", sum);
Ok(())
}