aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock4
-rw-r--r--Cargo.toml2
-rw-r--r--day3/Cargo.toml6
-rw-r--r--day3/src/main.rs72
4 files changed, 83 insertions, 1 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 2e0bc8d..f04604e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -18,3 +18,7 @@ dependencies = [
[[package]]
name = "day2"
version = "0.1.0"
+
+[[package]]
+name = "day3"
+version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
index d8418cc..4f07ca2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,3 +1,3 @@
[workspace]
resolver = "3"
-members = ["day1", "day2"]
+members = ["day1", "day2", "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<u8>);
+
+struct ParseBatteryBankError;
+
+impl From<ParseIntError> for ParseBatteryBankError {
+ fn from(_: ParseIntError) -> Self {
+ Self
+ }
+}
+
+impl FromStr for BatteryBank {
+ type Err = ParseBatteryBankError;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ Ok(Self(
+ s.chars()
+ .map(|ch| ch.to_string().parse())
+ .collect::<Result<_, _>>()?,
+ ))
+ }
+}
+
+#[derive(Debug)]
+struct Input(Vec<BatteryBank>);
+
+#[derive(Debug)]
+enum ParseInputError {
+ BadBank(String),
+}
+
+impl FromStr for Input {
+ type Err = ParseInputError;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ Ok(Self(
+ s.split_whitespace()
+ .map(|bank| bank.parse().map_err(|_| ParseInputError::BadBank(s.into())))
+ .collect::<Result<_, _>>()?,
+ ))
+ }
+}
+
+#[derive(Debug)]
+enum Error {
+ BadArgument,
+ BadInput(ParseInputError),
+}
+
+impl From<std::io::Error> for Error {
+ fn from(value: std::io::Error) -> Self {
+ Self::BadArgument
+ }
+}
+
+impl From<ParseInputError> 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(())
+}