Day 1
This commit is contained in:
parent
2109ac28d2
commit
df0aed7057
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aoc21"
|
||||
version = "0.1.0"
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,43 @@
|
|||
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);
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
mod day1;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
day1::day1();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue