AoC21/src/day1.rs

44 lines
946 B
Rust
Raw Normal View History

2021-12-01 05:19:46 +00:00
pub fn day1() {
// let input = r#"199
// 200
// 208
// 210
// 200
// 207
// 240
// 269
// 260
// 263"#;
let input = include_str!("../input/day1.txt");
let ints = input
.trim()
.lines()
.map(|s| s.trim().parse::<i32>().unwrap());
let mut iter = ints.clone();
let mut prev: i32 = iter.next().unwrap();
let mut count = 0;
for cur in iter {
if cur > prev {
count += 1;
}
prev = cur;
}
println!("count: {}", count);
let mut iter = ints.clone().zip(ints.clone().skip(1).zip(ints.skip(2)));
let first = iter.next().unwrap();
let mut prev_sum = first.0 + first.1 .0 + first.1 .1;
let mut new_count = 0;
for (a, (b, c)) in iter {
let sum = a + b + c;
if sum > prev_sum {
new_count += 1;
}
prev_sum = sum;
}
println!("new_count: {}", new_count);
}