summaryrefslogtreecommitdiff
path: root/2024_rust/src/bin/day3.rs
diff options
context:
space:
mode:
authorGuillermo Ramos2024-12-06 15:59:32 +0100
committerGuillermo Ramos2024-12-06 16:29:18 +0100
commitff064b6f13019b15346ff320a486d70b95d80b2a (patch)
tree396868cbbd9be95f6ce0a808f8436d4a1a0f1d89 /2024_rust/src/bin/day3.rs
parenta6029017bb354381c17afaf00526442966648c3f (diff)
downloadAoC-ff064b6f13019b15346ff320a486d70b95d80b2a.tar.gz
Each day is now a binary
Diffstat (limited to '2024_rust/src/bin/day3.rs')
-rw-r--r--2024_rust/src/bin/day3.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/2024_rust/src/bin/day3.rs b/2024_rust/src/bin/day3.rs
new file mode 100644
index 0000000..1eedefe
--- /dev/null
+++ b/2024_rust/src/bin/day3.rs
@@ -0,0 +1,37 @@
+use regex::Regex;
+
+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()
+}
+
+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()
+}
+
+fn main() {
+ aoc2024::run_day("3", p1, p2);
+}