diff --git a/2024/day3/part2/src/main.rs b/2024/day3/part2/src/main.rs new file mode 100644 index 0000000..8b04ac0 --- /dev/null +++ b/2024/day3/part2/src/main.rs @@ -0,0 +1,48 @@ +use regex::Regex; +use std::fs::read_to_string; + +fn main() { + let lines: Vec = read_to_string("input.txt") + .unwrap() + .lines() + .map(String::from) + .collect(); + let pattern1 = Regex::new(r"mul\((\d+,\d+)\)").unwrap(); + let pattern2 = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap(); + + let mut finalvec1 = mul_parse(lines, pattern1); + let finalint = mul_process(finalvec1,pattern2); + println!("{:?}",finalint) +} + + +fn mul_parse(thelines: Vec, pattern: Regex) -> Vec{ + let mut mulvec: Vec = Vec::new(); + for i in thelines { + let tempvec: Vec = + pattern.find_iter(&i) + .filter_map(|fr| fr.as_str().parse::().ok()).collect(); + + for i2 in tempvec { + mulvec.push(i2); + } + + } + mulvec + +} + +fn mul_process(mulvec: Vec, pattern: Regex) -> i64 { + let mut mulint: i64 = 0; + for i in mulvec { + let captures = pattern.captures(&i).unwrap(); + let capture1 = captures.get(1).unwrap().as_str().parse::().unwrap(); + + let capture2 = captures.get(2).unwrap().as_str().parse::().unwrap(); + mulint += (capture1 * capture2) as i64; + } + + + mulint + +} \ No newline at end of file