aoc2021 day 01

This commit is contained in:
Dan Ballard 2021-12-04 01:04:26 -08:00
commit bf9a293136
7 changed files with 2048 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
.idea

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc2021"
version = "0.1.0"

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "aoc2021"
version = "0.1.0"
edition = "2021"
authors = ["Dan Ballard <dan@mindstab.net>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1
README.md Normal file
View File

@ -0,0 +1 @@
`cargo t dayxx -- --nocapture`

2000
input/day01.txt Normal file

File diff suppressed because it is too large Load Diff

28
src/day01.rs Normal file
View File

@ -0,0 +1,28 @@
fn parse_input(input: &str) -> Vec<i32> {
input.lines().filter_map(|n| n.parse::<i32>().ok()).collect()
}
fn count_up(input: Vec<i32>) -> usize {
input.windows(2).filter(|w| w[1]> w[0] ).count()
}
fn count_up_window(input: Vec<i32>) -> usize {
input.windows(3).map(|w| w.iter().sum()).collect::<Vec<i32>>().windows(2).filter(|w| w[1] > w[0]).count()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part_1() {
println!("Part 1 Solution: {}", count_up(parse_input(include_str!("../input/day01.txt"))));
}
#[test]
fn part_2() {
println!("Part 2 Solution: {}", count_up_window(parse_input(include_str!("../input/day01.txt"))));
}
}

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod day01;