From a69f1e59006b6fd5709e470d9f5af54879cffde4 Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Thu, 4 Dec 2025 14:33:59 +0200 Subject: Day 3: parse input --- day3/Cargo.toml | 6 +++++ day3/src/main.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 day3/Cargo.toml create mode 100644 day3/src/main.rs (limited to 'day3') diff --git a/day3/Cargo.toml b/day3/Cargo.toml new file mode 100644 index 0000000..5111c7d --- /dev/null +++ b/day3/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day3" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day3/src/main.rs b/day3/src/main.rs new file mode 100644 index 0000000..dbbd303 --- /dev/null +++ b/day3/src/main.rs @@ -0,0 +1,72 @@ +use std::{env, fs, num::ParseIntError, str::FromStr}; + +#[derive(Debug)] +struct BatteryBank(Vec); + +struct ParseBatteryBankError; + +impl From for ParseBatteryBankError { + fn from(_: ParseIntError) -> Self { + Self + } +} + +impl FromStr for BatteryBank { + type Err = ParseBatteryBankError; + + fn from_str(s: &str) -> Result { + Ok(Self( + s.chars() + .map(|ch| ch.to_string().parse()) + .collect::>()?, + )) + } +} + +#[derive(Debug)] +struct Input(Vec); + +#[derive(Debug)] +enum ParseInputError { + BadBank(String), +} + +impl FromStr for Input { + type Err = ParseInputError; + + fn from_str(s: &str) -> Result { + Ok(Self( + s.split_whitespace() + .map(|bank| bank.parse().map_err(|_| ParseInputError::BadBank(s.into()))) + .collect::>()?, + )) + } +} + +#[derive(Debug)] +enum Error { + BadArgument, + BadInput(ParseInputError), +} + +impl From for Error { + fn from(value: std::io::Error) -> Self { + Self::BadArgument + } +} + +impl From for Error { + fn from(value: ParseInputError) -> Self { + Self::BadInput(value) + } +} + +fn main() -> Result<(), Error> { + let mut args = env::args(); + let input_path = args.nth(1).ok_or(Error::BadArgument)?; + let input: Input = fs::read_to_string(input_path)?.parse()?; + + dbg!(input); + + Ok(()) +} -- cgit v1.2.3