diff --git a/inputs/day1.sample.txt b/inputs/day1.sample.txt new file mode 100644 index 0000000..60ac9b8 --- /dev/null +++ b/inputs/day1.sample.txt @@ -0,0 +1,11 @@ +199 +200 +208 +210 +200 +207 +240 +269 +260 +263 + diff --git a/src/main.rs b/src/main.rs index 0f60a4c..0270747 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +#![feature(test)] +extern crate test; + const INPUT: &'static str = include_str!("../inputs/day1.txt"); fn depths() -> Vec { @@ -8,15 +11,39 @@ fn depths() -> Vec { .collect() } -fn main() { - let days = depths(); +fn solution(depths: &[u64]) -> i32 { let mut count = 0; - for set in days.windows(2) { - if set[1] > set[0] { + // For a window size of 3, We can consider a window size of 4 + // A + B + C && B + C + D + // Since B, C are shared between the two consecutive sets, + // We only need to compare A and D + for set in depths.windows(4) { + if set[0] < set[3] { count += 1; } } + count +} + +fn main() { + let depths = &depths(); + let count = solution(depths); println!("increased {} times", count); } + +#[bench] +fn solution_bench(b: &mut test::Bencher) { + let depths = depths(); + + b.iter(|| { + let v = solution(&depths); + test::black_box(v); + }) +} + +#[test] +fn solution_test() { + assert_eq!(solution(&depths()), 1429); +}