From 6e3705e763f285f877d0b83cbe0c7fa398f9c71d Mon Sep 17 00:00:00 2001 From: kleph Date: Sat, 2 Dec 2023 13:51:49 +0100 Subject: [PATCH] 2023 - Day 1 part 1 - rust --- 2023/1/1.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2023/1/1.rs diff --git a/2023/1/1.rs b/2023/1/1.rs new file mode 100644 index 0000000..af437dd --- /dev/null +++ b/2023/1/1.rs @@ -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::().unwrap(); + } + + println!("Total lines are: {}", accum); +}