generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.rs
56 lines (50 loc) · 1.6 KB
/
03.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use regex::Regex;
advent_of_code::solution!(3);
pub fn part_one(input: &str) -> Option<u32> {
let regex = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap();
Some(regex.captures_iter(input)
.map(|capture| {
let x = capture.get(1).unwrap().as_str().parse::<u32>().unwrap();
let y = capture.get(2).unwrap().as_str().parse::<u32>().unwrap();
x * y
})
.sum())
}
pub fn part_two(input: &str) -> Option<u32> {
let regex = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)").unwrap();
let mut enabled = true;
Some(regex.captures_iter(input)
.map(|capture| {
match capture.get(0).unwrap().as_str() {
"do()" => {
enabled = true;
0
},
"don't()" => {
enabled = false;
0
},
_ if enabled => {
let x = capture.get(1).unwrap().as_str().parse::<u32>().unwrap();
let y = capture.get(2).unwrap().as_str().parse::<u32>().unwrap();
x * y
},
_ => 0,
}
})
.sum())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file_part("examples", DAY, 1));
assert_eq!(result, Some(161));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file_part("examples", DAY, 2));
assert_eq!(result, Some(48));
}
}