day 3 part 2

This commit is contained in:
endernon 2024-12-03 19:02:18 +00:00
parent 716770ea20
commit 49311e3637
3 changed files with 92 additions and 10 deletions

54
2024/day3/part2/Cargo.lock generated Normal file
View file

@ -0,0 +1,54 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "day3"
version = "0.1.0"
dependencies = [
"regex",
]
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"

View file

@ -0,0 +1,7 @@
[package]
name = "day3"
version = "0.1.0"
edition = "2021"
[dependencies]
regex = "1.11.1"

View file

@ -7,12 +7,11 @@ fn main() {
.lines() .lines()
.map(String::from) .map(String::from)
.collect(); .collect();
let pattern1 = Regex::new(r"mul\((\d+,\d+)\)").unwrap(); let pattern1 = Regex::new(r"(mul\((\d+)\,(\d+)\))|(do\(\))|(don\'t\(\))").unwrap();
let pattern2 = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap(); let finalvec1 = mul_parse(lines, pattern1.clone());
let finalint = mul_process(finalvec1,pattern1.clone());
let mut finalvec1 = mul_parse(lines, pattern1); // println!("{:?}",finalint)
let finalint = mul_process(finalvec1,pattern2); println!("{finalint}")
println!("{:?}",finalint)
} }
@ -28,18 +27,40 @@ fn mul_parse(thelines: Vec<String>, pattern: Regex) -> Vec<String>{
} }
} }
println!("mulvec is {:?}",mulvec);
mulvec mulvec
} }
fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 { fn mul_process(mulvec: Vec<String>, pattern: Regex) -> i64 {
let mut enabled = true;
let mut mulint: i64 = 0; let mut mulint: i64 = 0;
let matchmul = Regex::new(r"mul\((\d+)\,(\d+)\)").unwrap();
let matchdo = Regex::new(r"do\(\)").unwrap();
let matchdont = Regex::new(r"don\'t\(\)").unwrap();
for i in mulvec { for i in mulvec {
let captures = pattern.captures(&i).unwrap(); if let Some(t) = matchdo.find(&i) {
let capture1 = captures.get(1).unwrap().as_str().parse::<i32>().unwrap(); println!("matchdo");
enabled = true;
}
else if let Some(t) = matchdont.find(&i) {
println!("matchdont");
enabled = false;
}
else if let Some(t) = matchmul.find(&i) {
if enabled {
println!("matchmul");
let captures = pattern.captures(&i).unwrap();
let capture1 = captures.get(2).unwrap().as_str().parse::<i32>().unwrap();
let capture2 = captures.get(3).unwrap().as_str().parse::<i32>().unwrap();
mulint += (capture1 * capture2) as i64;
}
}
let capture2 = captures.get(2).unwrap().as_str().parse::<i32>().unwrap();
mulint += (capture1 * capture2) as i64;
} }