From a014b25947af141b465f57dd8d1c5b14148cb382 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 26 Nov 2022 21:39:02 -0500 Subject: [PATCH] Demonstrate solving the first problems from 2021 --- .gitignore | 1 + problems/Cargo.lock | 7 +++++++ problems/Cargo.toml | 8 ++++++++ problems/data/day1a.txt | 10 ++++++++++ problems/src/day1.rs | 25 +++++++++++++++++++++++++ problems/src/main.rs | 14 ++++++++++++++ 6 files changed, 65 insertions(+) create mode 100644 problems/Cargo.lock create mode 100644 problems/Cargo.toml create mode 100644 problems/data/day1a.txt create mode 100644 problems/src/day1.rs create mode 100644 problems/src/main.rs diff --git a/.gitignore b/.gitignore index 92b2793..bd32e74 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .direnv +target diff --git a/problems/Cargo.lock b/problems/Cargo.lock new file mode 100644 index 0000000..5cceaa4 --- /dev/null +++ b/problems/Cargo.lock @@ -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" diff --git a/problems/Cargo.toml b/problems/Cargo.toml new file mode 100644 index 0000000..7bf1070 --- /dev/null +++ b/problems/Cargo.toml @@ -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] diff --git a/problems/data/day1a.txt b/problems/data/day1a.txt new file mode 100644 index 0000000..167e291 --- /dev/null +++ b/problems/data/day1a.txt @@ -0,0 +1,10 @@ +199 +200 +208 +210 +200 +207 +240 +269 +260 +263 diff --git a/problems/src/day1.rs b/problems/src/day1.rs new file mode 100644 index 0000000..5afd767 --- /dev/null +++ b/problems/src/day1.rs @@ -0,0 +1,25 @@ +const PART1: &str = include_str!("../data/day1a.txt"); + +pub fn part1() -> String { + let lines = PART1.lines(); + + let depths: Vec = lines.map(|l| l.parse::().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() +} diff --git a/problems/src/main.rs b/problems/src/main.rs new file mode 100644 index 0000000..1ca43ac --- /dev/null +++ b/problems/src/main.rs @@ -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); +}