generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.rs
75 lines (65 loc) · 2.16 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
advent_of_code::solution!(3);
use regex::Regex;
pub fn part_one(input: &str) -> Option<u32> {
// let pattern = r"mul\(\d\(+\),\s*\d\(+\)\)"; // Corrected pattern
let pattern = r"mul\((\d+),\s*(\d+)\)"; // Matches mul(...) with one or more digits
let regex = Regex::new(pattern).unwrap(); // Create the regex object
// let mut matches = Vec::new();
// for mat in regex.find_iter(input) {
// matches.push(mat.as_str());
// }
// println!("{:?}", matches);
let mut total = 0;
for mat in regex.captures_iter(input) {
let first = mat[1].parse::<u32>().unwrap();
let second = mat[2].parse::<u32>().unwrap();
let mul_result = first * second;
total += mul_result;
}
// let mul = input.find("mul(\d{3},{3})").iter().collect();
// println!("{:?}", mul);
Some(total)
}
pub fn part_two(input: &str) -> Option<u32> {
let pattern = Regex::new(r"mul\((\d+),(\d+)\)|don't\(\)|do\(\)").unwrap();
let mut is_enabled = true;
let result = pattern
.captures_iter(input)
.filter_map(|captures| {
if let (Some(x), Some(y)) = (captures.get(1), captures.get(2)) {
if is_enabled {
let x = x.as_str().parse::<u32>().unwrap();
let y = y.as_str().parse::<u32>().unwrap();
// println!("{} ({}, {})", is_enabled, x, y);
Some(x * y)
} else {
None
}
} else {
match &captures[0] {
"don't()" => is_enabled = false,
"do()" => is_enabled = true,
_ => {}
}
None
}
})
.sum::<u32>();
Some(result)
}
#[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_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(48));
}
}