feat: complete day 1

This commit is contained in:
2021-12-01 08:25:25 -04:00
parent 2fd66c43ba
commit ca9eb632b8
6 changed files with 71 additions and 6 deletions

View File

@@ -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);
}
}

18
rust/2021/src/day_02.rs Normal file
View File

@@ -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 {}

View File

@@ -1,5 +1,6 @@
mod day_01;
mod day_02;
pub fn run(input: String) {
day_01::run(input);
day_02::run(input);
}

View File

@@ -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);
}