2023 - Day 1 part 1 - rust
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a29c2f44f7
commit
6e3705e763
1 changed files with 31 additions and 0 deletions
31
2023/1/1.rs
Normal file
31
2023/1/1.rs
Normal file
|
@ -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::<u32>().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Total lines are: {}", accum);
|
||||||
|
}
|
Loading…
Reference in a new issue