From ca9eb632b8d064c20788c907f74184b41a335aea Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Wed, 1 Dec 2021 08:25:25 -0400 Subject: [PATCH] feat: complete day 1 --- rust/2021/Cargo.lock | 18 ++++++++++++++++++ rust/2021/Cargo.toml | 1 + rust/2021/src/day_01.rs | 35 +++++++++++++++++++++++++++++++---- rust/2021/src/day_02.rs | 18 ++++++++++++++++++ rust/2021/src/lib.rs | 3 ++- rust/2021/src/main.rs | 2 +- 6 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 rust/2021/src/day_02.rs diff --git a/rust/2021/Cargo.lock b/rust/2021/Cargo.lock index 260f8dd..b189704 100644 --- a/rust/2021/Cargo.lock +++ b/rust/2021/Cargo.lock @@ -5,3 +5,21 @@ version = 3 [[package]] name = "advent-of-code-2021" version = "0.1.0" +dependencies = [ + "itertools", +] + +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + +[[package]] +name = "itertools" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf" +dependencies = [ + "either", +] diff --git a/rust/2021/Cargo.toml b/rust/2021/Cargo.toml index 3da452d..07b612d 100644 --- a/rust/2021/Cargo.toml +++ b/rust/2021/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +itertools = "0.10.1" diff --git a/rust/2021/src/day_01.rs b/rust/2021/src/day_01.rs index d24ff15..9827a56 100644 --- a/rust/2021/src/day_01.rs +++ b/rust/2021/src/day_01.rs @@ -1,15 +1,42 @@ -pub fn solve_part1(input: &str) -> &str { +use itertools::Itertools; + +fn solve_part1(input: &str) -> i32 { input + .lines() + .filter_map(|s| s.parse().ok()) + .tuple_windows::<(i32, i32)>() + .fold(0, |a, (l, r)| a + if l < r { 1 } else { 0 }) } -pub fn solve_part2(input: &str) -> &str { +fn solve_part2(input: &str) -> i32 { input + .lines() + .filter_map(|s| s.parse().ok()) + .tuple_windows::<(i32, i32, i32)>() + .map(|(l, m, r)| l + m + r) + .tuple_windows() + .fold(0, |a, (l, r)| a + if l < r { 1 } else { 0 }) } pub fn run(input: String) { println!("--- Part 1 ---"); println!("Answer: {}", solve_part1(&input)); - print!("\n"); + println!(); println!("--- Part 2 ---"); - println!("Answer: {}", solve_part1(&input)) + println!("Answer: {}", solve_part2(&input)) +} + +#[cfg(test)] +mod tests { + const EXAMPLE_INPUT: &str = "199\n200\n208\n210\n200\n207\n240\n269\n260\n263"; + + #[test] + fn part1() { + assert_eq!(super::solve_part1(EXAMPLE_INPUT), 7); + } + + #[test] + fn part2() { + assert_eq!(super::solve_part2(EXAMPLE_INPUT), 5); + } } diff --git a/rust/2021/src/day_02.rs b/rust/2021/src/day_02.rs new file mode 100644 index 0000000..8e50bab --- /dev/null +++ b/rust/2021/src/day_02.rs @@ -0,0 +1,18 @@ +fn solve_part1(input: &str) -> i32 { + todo!() +} + +fn solve_part2(input: &str) -> i32 { + todo!() +} + +pub fn run(input: String) { + println!("--- Part 1 ---"); + println!("Answer: {}", solve_part1(&input)); + println!(); + println!("--- Part 2 ---"); + println!("Answer: {}", solve_part2(&input)) +} + +#[cfg(test)] +mod tests {} diff --git a/rust/2021/src/lib.rs b/rust/2021/src/lib.rs index 61b873e..528d39d 100644 --- a/rust/2021/src/lib.rs +++ b/rust/2021/src/lib.rs @@ -1,5 +1,6 @@ mod day_01; +mod day_02; pub fn run(input: String) { - day_01::run(input); + day_02::run(input); } diff --git a/rust/2021/src/main.rs b/rust/2021/src/main.rs index 7a36d05..a7c7479 100644 --- a/rust/2021/src/main.rs +++ b/rust/2021/src/main.rs @@ -1,4 +1,4 @@ fn main() { - let input = std::fs::read_to_string("./input/2020/day20.txt").expect("open input file"); + let input = std::fs::read_to_string("./input/2021/day2.txt").expect("open input file"); advent_of_code_2021::run(input); }