commit 0bc8636003f98c02c9209007bfa53a6b61f5a01f Author: BoolPurist Date: Thu Aug 15 21:21:35 2024 +0200 Implemented cli framework advent of code puzzels diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6824097 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,384 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "advent_of_code_2023" +version = "0.1.0" +dependencies = [ + "clap", + "derive_more", + "env_logger", + "log", + "regex", + "thiserror", +] + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "216aec2b177652e3846684cbfe25c9964d18ec45234f0f5da5157b207ed1aab6" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "derive_more" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-segmentation" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" + +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2e3b037 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "advent_of_code_2023" +version = "0.1.0" +edition = "2021" + +[dependencies] +derive_more = { version = "1", features = ["full"] } +regex = "1.10.6" +clap = { version = "4.5.15", features = ["derive"] } +env_logger = "0.11.5" +log = "0.4.22" +thiserror = "1.0.63" diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..af8e1bb --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,31 @@ +use clap::Parser; +mod given_day; +mod given_task; + +pub use given_day::GivenDay; +pub use given_task::GivenTask; + +#[derive(Debug, Parser)] +pub struct AppCliArguments { + #[arg(short, long)] + day: GivenDay, + #[arg(short, long)] + task: GivenTask, + #[arg(short, long)] + read_as_file: bool, + input: String, +} + +impl AppCliArguments { + pub fn day(&self) -> GivenDay { + self.day + } + + pub fn task(&self) -> GivenTask { + self.task + } + + pub fn input(&self) -> &str { + &self.input + } +} diff --git a/src/cli/given_day.rs b/src/cli/given_day.rs new file mode 100644 index 0000000..7575ca6 --- /dev/null +++ b/src/cli/given_day.rs @@ -0,0 +1,77 @@ +use std::{str::FromStr, sync::Arc}; + +use derive_more::derive::Into; +use thiserror::Error; + +use crate::constants; + +#[derive(Debug, Error, PartialEq, Eq)] +pub enum InvalidGivenDayError { + #[error( + "({0}) is not a valid format for unsigned whole number between 0 and {}", + constants::MAX_DAY + )] + InvalidFormat(Arc), + #[error("({0}) is within the valid range between 0 and {}", constants::MAX_DAY)] + InvalidRange(u32), +} + +#[derive(Debug, Copy, Clone, Into, PartialEq, Eq)] +pub struct GivenDay(u32); + +impl GivenDay { + pub fn new(value: u32) -> Result { + if value < 1 || value > constants::MAX_DAY { + Err(InvalidGivenDayError::InvalidRange(value)) + } else { + Ok(Self(value)) + } + } +} +impl FromStr for GivenDay { + type Err = InvalidGivenDayError; + + fn from_str(s: &str) -> Result { + let number: u32 = s + .parse() + .map_err(|_| InvalidGivenDayError::InvalidFormat(Arc::from(s)))?; + + GivenDay::new(number) + } +} + +#[cfg(test)] +mod testing { + use crate::constants::{self, MAX_DAY}; + + use super::*; + + #[test] + fn parse_correct_value() { + fn assert_case(input: &str, expected: u32) { + let actual = GivenDay::from_str(input).unwrap(); + assert_eq!(expected, actual.into(), "input: {}", input); + } + + assert_case("1", 1); + assert_case("3", 3); + assert_case(&constants::MAX_DAY.to_string(), MAX_DAY); + } + + #[test] + fn detect_invalid_input() { + fn assert_case(input: &str, expected_err: InvalidGivenDayError) { + let actual = GivenDay::from_str(input).unwrap_err(); + assert_eq!(expected_err, actual, "input: {}", input); + } + + const OVER_MAX: u32 = constants::MAX_DAY + 1; + assert_case("0", InvalidGivenDayError::InvalidRange(0)); + assert_case( + &OVER_MAX.to_string(), + InvalidGivenDayError::InvalidRange(OVER_MAX), + ); + assert_case("", InvalidGivenDayError::InvalidFormat("".into())); + assert_case("a", InvalidGivenDayError::InvalidFormat("a".into())); + } +} diff --git a/src/cli/given_task.rs b/src/cli/given_task.rs new file mode 100644 index 0000000..0b58ae3 --- /dev/null +++ b/src/cli/given_task.rs @@ -0,0 +1,82 @@ +use std::{str::FromStr, sync::Arc}; + +use derive_more::derive::Into; +use thiserror::Error; + +use crate::constants; +#[derive(Debug, Error, PartialEq, Eq)] +pub enum InvalidGivenTaskError { + #[error( + "({0}) is not a valid format for unsigned whole number between 1 and {}", + constants::MAX_TASK + )] + InvalidFormat(Arc), + #[error( + "({0}) is within the valid range between 1 and {}", + constants::MAX_TASK + )] + InvalidRange(u32), +} + +#[derive(Debug, Copy, Into, Clone, PartialEq, Eq)] +pub struct GivenTask(u32); + +impl GivenTask { + pub fn new(value: u32) -> Result { + if value < 1 || value > constants::MAX_TASK { + Err(InvalidGivenTaskError::InvalidRange(value)) + } else { + Ok(Self(value)) + } + } +} + +impl FromStr for GivenTask { + type Err = InvalidGivenTaskError; + + fn from_str(s: &str) -> Result { + let number: u32 = s + .parse() + .map_err(|_| InvalidGivenTaskError::InvalidFormat(Arc::from(s)))?; + + GivenTask::new(number) + } +} + +#[cfg(test)] +mod testing { + use crate::{ + cli::GivenDay, + constants::{self, MAX_DAY}, + }; + + use super::*; + + #[test] + fn parse_correct_value() { + fn assert_case(input: &str, expected: u32) { + let actual = GivenDay::from_str(input).unwrap(); + assert_eq!(expected, actual.into(), "input: {}", input); + } + + assert_case("1", 1); + assert_case(&constants::MAX_DAY.to_string(), MAX_DAY); + } + + #[test] + fn detect_invalid_input() { + fn assert_case(input: &str, expected_err: InvalidGivenTaskError) { + let actual = GivenTask::from_str(input).unwrap_err(); + assert_eq!(expected_err, actual, "input: {}", input); + } + + const OVER_MAX: u32 = constants::MAX_TASK + 1; + assert_case("0", InvalidGivenTaskError::InvalidRange(0)); + assert_case( + &OVER_MAX.to_string(), + InvalidGivenTaskError::InvalidRange(OVER_MAX), + ); + assert_case("", InvalidGivenTaskError::InvalidFormat("".into())); + assert_case("a", InvalidGivenTaskError::InvalidFormat("a".into())); + } +} diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..7064e4d --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,2 @@ +pub const MAX_DAY: u32 = 25; +pub const MAX_TASK: u32 = 2; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5551262 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +pub mod cli; +pub mod constants; +pub mod solutions; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9f9f381 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,47 @@ +use std::process::ExitCode; + +use advent_of_code_2023::{cli::AppCliArguments, solutions}; +use clap::Parser; + +use thiserror::Error; + +fn main() -> ExitCode { + let args = AppCliArguments::parse(); + let solution = solve_given(&args); + match solution { + Ok(found_solution) => { + println!("{}", found_solution); + ExitCode::SUCCESS + } + Err(error) => { + eprintln!("{}", error); + ExitCode::FAILURE + } + } +} + +fn solve_given(args: &AppCliArguments) -> Result { + let all_solutions = solutions::create_solutions(); + + let found_task = { + let day: u32 = args.day().into(); + let task: u32 = args.task().into(); + let found_day = all_solutions + .get(day.saturating_sub(1) as usize) + .ok_or_else(|| NoSolutionFound::DayNotFound(day))?; + found_day + .get(task.saturating_sub(1) as usize) + .ok_or_else(|| NoSolutionFound::TaskNotFound { day, task }) + }?; + + let solved = (found_task)(args.input()); + Ok(solved) +} + +#[derive(Debug, Clone, Copy, Error)] +enum NoSolutionFound { + #[error("There is no solution for the day {0}")] + DayNotFound(u32), + #[error("There is not solution for task {task} under the day {day}")] + TaskNotFound { day: u32, task: u32 }, +} diff --git a/src/solutions.rs b/src/solutions.rs new file mode 100644 index 0000000..be2e7a6 --- /dev/null +++ b/src/solutions.rs @@ -0,0 +1,32 @@ +pub mod day5; + +pub fn create_solutions() -> Vec String>> { + vec![ + vec![not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![day5::solve_task_1, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + vec![not_implemented_yet, not_implemented_yet], + ] +} + +fn not_implemented_yet(_: &str) -> String { + todo!("There is not implementation for the given task under given day") +} diff --git a/src/solutions/day5.rs b/src/solutions/day5.rs new file mode 100644 index 0000000..d6cbcfc --- /dev/null +++ b/src/solutions/day5.rs @@ -0,0 +1,7 @@ +pub fn solve_task_1(input: &str) -> String { + todo!() +} + +fn parse(input: &str) -> ! { + todo!() +}