This commit is contained in:
endernon 2024-12-03 20:18:39 +00:00
parent eab914518a
commit 84bd29b5b4
3 changed files with 19 additions and 34 deletions

View file

@ -11,25 +11,23 @@ fn main() {
let pattern2 = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap(); let pattern2 = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap();
let mut finalvec1 = mul_parse(lines, pattern1); let mut finalvec1 = mul_parse(lines, pattern1);
let finalint = mul_process(finalvec1,pattern2); let finalint = mul_process(finalvec1, pattern2);
println!("{:?}",finalint) println!("{:?}", finalint)
} }
fn mul_parse(thelines: Vec<String>, pattern: Regex) -> Vec<String> {
fn mul_parse(thelines: Vec<String>, pattern: Regex) -> Vec<String>{
let mut mulvec: Vec<String> = Vec::new(); let mut mulvec: Vec<String> = Vec::new();
for i in thelines { for i in thelines {
let tempvec: Vec<String> = let tempvec: Vec<String> = pattern
pattern.find_iter(&i) .find_iter(&i)
.filter_map(|fr| fr.as_str().parse::<String>().ok()).collect(); .filter_map(|fr| fr.as_str().parse::<String>().ok())
.collect();
for i2 in tempvec { for i2 in tempvec {
mulvec.push(i2); mulvec.push(i2);
} }
} }
mulvec mulvec
} }
fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 { fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 {
@ -42,7 +40,5 @@ fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 {
mulint += (capture1 * capture2) as i64; mulint += (capture1 * capture2) as i64;
} }
mulint mulint
}
}

View file

@ -9,27 +9,25 @@ fn main() {
.collect(); .collect();
let pattern1 = Regex::new(r"(mul\((\d+)\,(\d+)\))|(do\(\))|(don\'t\(\))").unwrap(); let pattern1 = Regex::new(r"(mul\((\d+)\,(\d+)\))|(do\(\))|(don\'t\(\))").unwrap();
let finalvec1 = mul_parse(lines, pattern1.clone()); let finalvec1 = mul_parse(lines, pattern1.clone());
let finalint = mul_process(finalvec1,pattern1.clone()); let finalint = mul_process(finalvec1, pattern1.clone());
// println!("{:?}",finalint) // println!("{:?}",finalint)
println!("{finalint}") println!("{finalint}")
} }
fn mul_parse(thelines: Vec<String>, pattern: Regex) -> Vec<String> {
fn mul_parse(thelines: Vec<String>, pattern: Regex) -> Vec<String>{
let mut mulvec: Vec<String> = Vec::new(); let mut mulvec: Vec<String> = Vec::new();
for i in thelines { for i in thelines {
let tempvec: Vec<String> = let tempvec: Vec<String> = pattern
pattern.find_iter(&i) .find_iter(&i)
.filter_map(|fr| fr.as_str().parse::<String>().ok()).collect(); .filter_map(|fr| fr.as_str().parse::<String>().ok())
.collect();
for i2 in tempvec { for i2 in tempvec {
mulvec.push(i2); mulvec.push(i2);
} }
} }
println!("mulvec is {:?}",mulvec); println!("mulvec is {:?}", mulvec);
mulvec mulvec
} }
fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 { fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 {
@ -42,12 +40,10 @@ fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 {
if let Some(t) = matchdo.find(&i) { if let Some(t) = matchdo.find(&i) {
println!("matchdo"); println!("matchdo");
enabled = true; enabled = true;
} } else if let Some(t) = matchdont.find(&i) {
else if let Some(t) = matchdont.find(&i) {
println!("matchdont"); println!("matchdont");
enabled = false; enabled = false;
} } else if let Some(t) = matchmul.find(&i) {
else if let Some(t) = matchmul.find(&i) {
if enabled { if enabled {
println!("matchmul"); println!("matchmul");
let captures = pattern.captures(&i).unwrap(); let captures = pattern.captures(&i).unwrap();
@ -56,14 +52,7 @@ fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 {
mulint += (capture1 * capture2) as i64; mulint += (capture1 * capture2) as i64;
} }
} }
} }
mulint mulint
}
}

View file

@ -1,3 +1,3 @@
# My solutions for Advent of Code # My solutions for Advent of Code
exactly what the title says, starting 2024, since I feel this would be a great way to learn rust. exactly what the title says, starting 2024, since I feel this would be a great way to learn rust.cc