From d9363b9aa8e03a4643effa6cd95892ef76b955dd Mon Sep 17 00:00:00 2001 From: Varun Ramakrishnan Date: Wed, 4 Dec 2024 00:46:30 -0500 Subject: [PATCH] Solve Day 3 Part 1 --- data/examples/03.txt | 1 + src/bin/03.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 data/examples/03.txt create mode 100644 src/bin/03.rs diff --git a/data/examples/03.txt b/data/examples/03.txt new file mode 100644 index 0000000..f274bda --- /dev/null +++ b/data/examples/03.txt @@ -0,0 +1 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) diff --git a/src/bin/03.rs b/src/bin/03.rs new file mode 100644 index 0000000..ef401e9 --- /dev/null +++ b/src/bin/03.rs @@ -0,0 +1,38 @@ +advent_of_code::solution!(3); + +use regex::Regex; + +pub fn part_one(input: &str) -> Option { + let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap(); + let result = re + .captures_iter(input) + .filter_map(|x| { + let first = x[1].parse::().ok()?; + let second = x[2].parse::().ok()?; + Some(first * second) + }) + .sum(); + + Some(result) +} + +pub fn part_two(input: &str) -> Option { + None +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(161)); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); + } +}