Day 1
This commit is contained in:
commit
8350d5d70b
5 changed files with 2051 additions and 0 deletions
33
src/bin/01.rs
Normal file
33
src/bin/01.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
const INPUT: &str = include_str!("../../input/01a.txt");
|
||||
|
||||
fn main() {
|
||||
let nums = input();
|
||||
let count = count_increases(nums);
|
||||
println!("First solution: {}", count);
|
||||
|
||||
let input: Vec<i32> = input().collect();
|
||||
let nums = input.windows(3).map(|w| w.iter().sum());
|
||||
let count = count_increases(nums);
|
||||
println!("Second solution: {}", count);
|
||||
}
|
||||
|
||||
fn count_increases(it: impl Iterator<Item = i32>) -> usize {
|
||||
let mut count = 0;
|
||||
let mut prev = None;
|
||||
|
||||
for n in it {
|
||||
if let Some(prev) = prev {
|
||||
if n > prev {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
prev = Some(n);
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
|
||||
fn input() -> impl Iterator<Item = i32> {
|
||||
INPUT.lines().map(|l| l.parse::<i32>().unwrap())
|
||||
}
|
8
src/lib.rs
Normal file
8
src/lib.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = 2 + 2;
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue