-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.rs
68 lines (60 loc) · 2.01 KB
/
day02.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
use regex::Regex;
pub fn solve_part_one(input: &String) -> i32 {
let re = Regex::new(r"(up|down|forward) (\d+)").unwrap();
let (horizontal, depth) =
re.captures_iter(input)
.fold((0, 0), |(horizontal, depth), capture| {
let delta = capture.get(2).unwrap().as_str().parse::<i32>().unwrap();
match capture.get(1).unwrap().as_str() {
"forward" => (horizontal + delta, depth),
"down" => (horizontal, depth + delta),
"up" => (horizontal, depth - delta),
_ => unreachable!(),
}
});
horizontal * depth
}
pub fn solve_part_two(input: &String) -> i32 {
let re = Regex::new(r"(up|down|forward) (\d+)").unwrap();
let (horizontal, depth, _) =
re.captures_iter(input)
.fold((0, 0, 0), |(horizontal, depth, aim), capture| {
let delta = capture.get(2).unwrap().as_str().parse::<i32>().unwrap();
match capture.get(1).unwrap().as_str() {
"forward" => (horizontal + delta, depth + aim * delta, aim),
"down" => (horizontal, depth, aim + delta),
"up" => (horizontal, depth, aim - delta),
_ => unreachable!(),
}
});
horizontal * depth
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::{solve_part_one, solve_part_two};
#[rstest]
#[case(indoc::indoc ! {"
forward 5
down 5
forward 8
up 3
down 8
forward 2
"}.to_string(), 150)]
fn test_part_one(#[case] input: String, #[case] expected: i32) {
assert_eq!(expected, solve_part_one(&input))
}
#[rstest]
#[case(indoc::indoc ! {"
forward 5
down 5
forward 8
up 3
down 8
forward 2
"}.to_string(), 900)]
fn test_part_two(#[case] input: String, #[case] expected: i32) {
assert_eq!(expected, solve_part_two(&input))
}
}