-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.rs
57 lines (50 loc) · 1.09 KB
/
day01.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
#![feature(test)]
#![feature(array_windows)]
extern crate test;
const INPUT: &str = include_str!("../inputs/input01.txt");
fn part1() -> u32 {
INPUT
.lines()
.map(|n| n.parse().unwrap())
.collect::<Vec<i32>>()
.array_windows()
.filter(|[a, b]| a < b)
.count() as u32
}
fn part2() -> u32 {
INPUT
.lines()
.map(|n| n.parse().unwrap())
.collect::<Vec<i32>>()
.array_windows()
.map(|[a, b, c]| a + b + c)
.collect::<Vec<i32>>()
.array_windows()
.filter(|[a, b]| a < b)
.count() as u32
}
pub fn main() {
println!("Part 1: Answer {}", part1());
println!("Part 2: Answer {} ", part2());
}
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[test]
fn part1_test() {
assert_eq!(part1(), 1581);
}
#[test]
fn part2_test() {
assert_eq!(part2(), 1618);
}
#[bench]
fn part1_bench(b: &mut Bencher) {
b.iter(|| part1());
}
#[bench]
fn part2_bench(b: &mut Bencher) {
b.iter(|| part2());
}
}