Demonstrate solving the first problems from 2021

This commit is contained in:
Savanni D'Gerinel 2022-11-26 21:39:02 -05:00
parent 29d691ce95
commit a014b25947
6 changed files with 65 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.direnv
target

7
problems/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 = "problems"
version = "0.1.0"

8
problems/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "problems"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

10
problems/data/day1a.txt Normal file
View File

@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263

25
problems/src/day1.rs Normal file
View File

@ -0,0 +1,25 @@
const PART1: &str = include_str!("../data/day1a.txt");
pub fn part1() -> String {
let lines = PART1.lines();
let depths: Vec<i32> = lines.map(|l| l.parse::<i32>().unwrap()).collect();
let depths_a = depths.iter();
let depths_b = depths.iter().skip(1);
let depth_cmp = depths_a.zip(depths_b);
let increases = depth_cmp.fold(0, |cur, (a, b)| {
println!("{} < {}? {}", a, b, a < b);
if a < b {
cur + 1
} else {
cur
}
});
format!("{}", increases)
}
pub fn part2() -> String {
"no response".to_owned()
}

14
problems/src/main.rs Normal file
View File

@ -0,0 +1,14 @@
mod day1;
fn main() {
let day = std::env::args().skip(1).next();
println!("day: {:?}", day);
let result = match day.as_ref().map(|v| v.as_ref()) {
Some("day1a") => day1::part1(),
Some("day1b") => day1::part2(),
_ => panic!("unrecognized day"),
};
println!("{:?} Result: {}", day, result);
}