diff options
Diffstat (limited to 'src/bin/2021/day1.rs')
-rw-r--r-- | src/bin/2021/day1.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/bin/2021/day1.rs b/src/bin/2021/day1.rs new file mode 100644 index 0000000..fce763c --- /dev/null +++ b/src/bin/2021/day1.rs @@ -0,0 +1,36 @@ +use advent_of_code::prelude::*; + +pub fn parse(fh: File) -> Result<Vec<i64>> { + Ok(parse::lines(fh).collect()) +} + +pub fn part1(ints: Vec<i64>) -> Result<usize> { + Ok(ints + .windows(2) + .map(|a| a[1] - a[0]) + .filter(|x| *x > 0) + .count()) +} + +pub fn part2(ints: Vec<i64>) -> Result<usize> { + Ok(ints + .windows(3) + .map(|a| a[0] + a[1] + a[2]) + .collect::<Vec<_>>() + .windows(2) + .map(|a| a[1] - a[0]) + .filter(|x| *x > 0) + .count()) +} + +#[test] +fn test() { + assert_eq!( + part1(parse(parse::data(2021, 1).unwrap()).unwrap()).unwrap(), + 1602 + ); + assert_eq!( + part2(parse(parse::data(2021, 1).unwrap()).unwrap()).unwrap(), + 1633 + ); +} |