aoc2021 day 01
This commit is contained in:
commit
bf9a293136
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
.idea
|
|
@ -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"
|
|
@ -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]
|
File diff suppressed because it is too large
Load Diff
|
@ -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"))));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod day01;
|
Loading…
Reference in New Issue