-
Notifications
You must be signed in to change notification settings - Fork 1
/
b.rs
52 lines (44 loc) · 1.01 KB
/
b.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
pub fn run() -> usize {
let input = include_str!("./input.txt");
return solve(input);
}
pub fn solve(input: &str) -> usize {
let (hpos, dep, _) = input
.lines()
.map(|cmd| {
let s = cmd.split_once(" ").unwrap();
return (s.0, s.1.parse::<usize>().unwrap());
})
.fold((0, 0, 0), |(hpos, dep, aim), (cmd, v)| {
match (cmd, v) {
("forward", v) => (hpos + v, dep + (aim * v), aim),
("down", v) => (hpos, dep, aim + v),
_ => (hpos, dep, aim - v),
}
});
return hpos * dep;
}
#[cfg(test)]
mod tests {
extern crate test;
use test::Bencher;
use super::*;
#[test]
fn test_example() {
let input = r"forward 5
down 5
forward 8
up 3
down 8
forward 2";
assert_eq!(solve(input), 900);
}
#[test]
fn test_puzzle() {
assert_eq!(run(), 1975421260);
}
#[bench]
fn bench_run(b: &mut Bencher) {
b.iter(|| run())
}
}