Skip to content

Commit

Permalink
Solve Day 3 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
destinationunknown committed Dec 4, 2024
1 parent 5710085 commit 2e99e81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions data/examples/03-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
22 changes: 19 additions & 3 deletions src/bin/03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ pub fn part_one(input: &str) -> Option<u32> {
}

pub fn part_two(input: &str) -> Option<u32> {
None
let without_newlines = input.replace('\n', "");
let dont_do_re = Regex::new(r"don't\(\).*?(do\(\)|$)").unwrap();
let mul_re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();

let filtered = dont_do_re.replace_all(&without_newlines, "");
let result: u32 = mul_re
.captures_iter(&filtered)
.filter_map(|x| {
let first = x[1].parse::<u32>().ok()?;
let second = x[2].parse::<u32>().ok()?;
Some(first * second)
})
.sum();

Some(result)
}

#[cfg(test)]
Expand All @@ -32,7 +46,9 @@ mod tests {

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(48));
}
}

0 comments on commit 2e99e81

Please sign in to comment.