summaryrefslogtreecommitdiff
path: root/2024_rust/src/day3.rs
diff options
context:
space:
mode:
authorGuillermo Ramos2024-12-04 10:28:51 +0100
committerGuillermo Ramos2024-12-04 10:51:41 +0100
commit1c9620d4666fdf41ef751f100dfb17f14133dd35 (patch)
treeece2ef499492b2426b13bd9b48f98f35a60123c2 /2024_rust/src/day3.rs
parenta42dc9c3efb52a74fc581835f585679e8a5e2bb1 (diff)
downloadAoC-1c9620d4666fdf41ef751f100dfb17f14133dd35.tar.gz
2024: move to single Rust crate
Diffstat (limited to '2024_rust/src/day3.rs')
-rw-r--r--2024_rust/src/day3.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/2024_rust/src/day3.rs b/2024_rust/src/day3.rs
new file mode 100644
index 0000000..cdcd62a
--- /dev/null
+++ b/2024_rust/src/day3.rs
@@ -0,0 +1,33 @@
+use regex::Regex;
+
+pub fn p1(input: &str) -> String {
+ let re = Regex::new(r"mul\(([0-9]{1,3}),([0-9]{1,3})\)").unwrap();
+
+ let mut result: u32 = 0;
+ for (_, [x, y]) in re.captures_iter(input).map(|cs| cs.extract()) {
+ result += x.parse::<u32>().unwrap() * y.parse::<u32>().unwrap();
+ }
+
+ result.to_string()
+}
+
+pub fn p2(input: &str) -> String {
+ let re = Regex::new(r"do\(\)|don't\(\)|mul\(([0-9]{1,3}),([0-9]{1,3})\)").unwrap();
+
+ let mut doing = true;
+ let mut result: u32 = 0;
+ for cs in re.captures_iter(input) {
+ let mut it = cs.iter().flatten().map(|m| m.as_str());
+ match it.next().unwrap() {
+ "do()" => doing = true,
+ "don't()" => doing = false,
+ mul if doing && mul.starts_with("mul") => {
+ let mut next = || it.next().unwrap().parse::<u32>().unwrap();
+ result += next() * next();
+ }
+ _ => (),
+ }
+ }
+
+ result.to_string()
+}