Day 01 Part 1

This commit is contained in:
2023-12-01 07:42:48 +00:00
parent af0b387339
commit eebfbe67e5
6 changed files with 1072 additions and 4 deletions

48
src/solutions/day01.rs Normal file
View File

@ -0,0 +1,48 @@
use super::dayxx::Solution;
use regex::Regex;
const REG: &str = r#"[0-9]"#;
pub struct Day01 {}
impl Solution for Day01 {
fn part1(
&self,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
let mut total: u32 = 0;
for line in input {
total += self.regex_get_num(line);
}
Ok(Box::new(total))
}
fn part2(
&self,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
Ok(Box::new(0))
}
fn get_day(&self) -> u8 {
1
}
}
impl Day01 {
fn regex_get_num(&self, input: &mut String) -> u32 {
let re = Regex::new(REG).unwrap();
// Get all single digits out of string
let matches: Vec<u32> = re
.find_iter(input)
.map(|m| m.as_str().parse::<u32>().unwrap_or_default())
.collect();
let mut num: u32 = matches.first().unwrap_or(&0) * 10;
num += matches.last().unwrap_or(&0);
num
}
}