aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Brattlof <hello@bryanbrattlof.com>2021-12-01 11:00:41 -0500
committerBryan Brattlof <hello@bryanbrattlof.com>2021-12-01 12:02:59 -0500
commitcd70d72b6986054ac19732484f3385bb19729d6b (patch)
treef0a61df9bacd87249bc2da0bd287f6495b4c45aa
parent509f2c8ab8d92571d6e545c4d9a7c3c4cc39f3d9 (diff)
downloadadvent-of-code-cd70d72b6986054ac19732484f3385bb19729d6b.tar.gz
advent-of-code-cd70d72b6986054ac19732484f3385bb19729d6b.tar.bz2
2021: day 1 part 2
-rw-r--r--y2021/src/d01.rs14
-rw-r--r--y2021/src/main.rs3
2 files changed, 17 insertions, 0 deletions
diff --git a/y2021/src/d01.rs b/y2021/src/d01.rs
index 0f1946c..673c958 100644
--- a/y2021/src/d01.rs
+++ b/y2021/src/d01.rs
@@ -7,6 +7,15 @@ pub fn part1<T: Iterator<Item = String>>(e: T) -> usize {
.count()
}
+pub fn part2<T: Iterator<Item = String>>(e: T) -> usize {
+ e.filter_map(|d| d.parse::<usize>().ok())
+ .tuple_windows::<(usize, usize, usize)>()
+ .map(|(x1, x2, x3)| x1 + x2 + x3 )
+ .tuple_windows()
+ .filter(|(s1, s2)| s1 < s2)
+ .count()
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -28,4 +37,9 @@ mod tests {
fn test() {
assert_eq!(7, part1(EX.iter().map(|x| x.to_string())));
}
+
+ #[test]
+ fn test2() {
+ assert_eq!(5, part2(EX.iter().map(|x| x.to_string())));
+ }
}
diff --git a/y2021/src/main.rs b/y2021/src/main.rs
index 8647836..d920269 100644
--- a/y2021/src/main.rs
+++ b/y2021/src/main.rs
@@ -16,5 +16,8 @@ fn main() {
if args.is_empty() || args.contains("1") {
let a = d01::part1(get_input_for_day(1));
println!("2021: day 1 part 1: {}", a);
+
+ let a = d01::part2(get_input_for_day(1));
+ println!("2021: day 1 part 2: {}", a);
}
}