feat: complete day 1

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

18
rust/2021/Cargo.lock generated
View File

@ -5,3 +5,21 @@ version = 3
[[package]] [[package]]
name = "advent-of-code-2021" name = "advent-of-code-2021"
version = "0.1.0" 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",
]

View File

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
itertools = "0.10.1"

View File

@ -1,15 +1,42 @@
pub fn solve_part1(input: &str) -> &str { use itertools::Itertools;
fn solve_part1(input: &str) -> i32 {
input 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 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) { pub fn run(input: String) {
println!("--- Part 1 ---"); println!("--- Part 1 ---");
println!("Answer: {}", solve_part1(&input)); println!("Answer: {}", solve_part1(&input));
print!("\n"); println!();
println!("--- Part 2 ---"); 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_01;
mod day_02;
pub fn run(input: String) { pub fn run(input: String) {
day_01::run(input); day_02::run(input);
} }

View File

@ -1,4 +1,4 @@
fn main() { 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); advent_of_code_2021::run(input);
} }