summaryrefslogtreecommitdiff
path: root/src/bin/day1.rs
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2021-12-01 18:51:11 +0100
committerReiner Herrmann <reiner@reiner-h.de>2021-12-01 18:53:50 +0100
commit473e100c9902351600e7bfe9f323c95946ca1cb0 (patch)
treeeebdb7426ce471fdf289929fb3857c4297ff9755 /src/bin/day1.rs
parent6a3bb87d33c9215bf1d33d9a57386ee9f3e50e9f (diff)
day1
Diffstat (limited to 'src/bin/day1.rs')
-rw-r--r--src/bin/day1.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/bin/day1.rs b/src/bin/day1.rs
new file mode 100644
index 0000000..eb4ee6a
--- /dev/null
+++ b/src/bin/day1.rs
@@ -0,0 +1,29 @@
+fn main() {
+ let input = advent::read_numbers(1);
+ println!("1a: {}", count_increasing(&input));
+ println!("1b: {}", count_increasing_window(&input, 3));
+}
+
+fn count_increasing(numbers: &[usize]) -> usize {
+ count_increasing_window(numbers, 1)
+}
+
+fn count_increasing_window(numbers: &[usize], window_size: usize) -> usize {
+ numbers[window_size..].iter()
+ .zip(numbers.iter())
+ .map(|(&x,&y)| (x as isize) - (y as isize))
+ .filter(|&x| x.is_positive())
+ .count()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test() {
+ let numbers = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
+ assert_eq!(count_increasing(&numbers), 7);
+ assert_eq!(count_increasing_window(&numbers, 3), 5);
+ }
+}