34 lines
850 B
Rust
34 lines
850 B
Rust
use std::fs;
|
|
|
|
fn extract_number(line: &str) -> u32 {
|
|
let mut has_digit: bool = false;
|
|
let mut first: u32 = 0;
|
|
let mut last: u32 = 0;
|
|
for character in line.chars() {
|
|
if character.is_digit(10) {
|
|
if !has_digit {
|
|
has_digit = true;
|
|
first = character.to_digit(10).unwrap();
|
|
}
|
|
last = character.to_digit(10).unwrap();
|
|
}
|
|
}
|
|
let number: u32 = first * 10 + last;
|
|
number
|
|
}
|
|
|
|
fn main() {
|
|
let input_filename: &str = "../data/input.txt";
|
|
|
|
let contents =
|
|
fs::read_to_string(input_filename).expect("Should have been able to read the file");
|
|
|
|
let mut sum: u32 = 0;
|
|
for line in contents.lines() {
|
|
let number = extract_number(line);
|
|
// println!("{:?}", number);
|
|
sum += number;
|
|
}
|
|
println!("{:?}", sum);
|
|
}
|