Skip to content

Commit d9363b9

Browse files
Solve Day 3 Part 1
1 parent 99a5a05 commit d9363b9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

data/examples/03.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))

src/bin/03.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
advent_of_code::solution!(3);
2+
3+
use regex::Regex;
4+
5+
pub fn part_one(input: &str) -> Option<u32> {
6+
let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
7+
let result = re
8+
.captures_iter(input)
9+
.filter_map(|x| {
10+
let first = x[1].parse::<u32>().ok()?;
11+
let second = x[2].parse::<u32>().ok()?;
12+
Some(first * second)
13+
})
14+
.sum();
15+
16+
Some(result)
17+
}
18+
19+
pub fn part_two(input: &str) -> Option<u32> {
20+
None
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
fn test_part_one() {
29+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
30+
assert_eq!(result, Some(161));
31+
}
32+
33+
#[test]
34+
fn test_part_two() {
35+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
36+
assert_eq!(result, None);
37+
}
38+
}

0 commit comments

Comments
 (0)