Skip to content

Commit 7973843

Browse files
committed
chore: add helper to split a string in two and parse each side
1 parent 65f2f90 commit 7973843

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

aoclp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod forth;
55
pub mod looping;
66
pub mod positioning;
77
pub mod solvers_impl;
8+
pub mod str;
89

910
pub type Error = anyhow::Error;
1011
pub type Result<T> = anyhow::Result<T>;

aoclp/src/str.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::str::FromStr;
2+
3+
pub trait StrHelper {
4+
fn split_parse_at<T, U>(&self, pos: usize) -> (T, U)
5+
where
6+
T: FromStr,
7+
U: FromStr,
8+
<T as FromStr>::Err: std::fmt::Debug,
9+
<U as FromStr>::Err: std::fmt::Debug;
10+
}
11+
12+
impl<S> StrHelper for S
13+
where
14+
S: AsRef<str>,
15+
{
16+
fn split_parse_at<T, U>(&self, pos: usize) -> (T, U)
17+
where
18+
T: FromStr,
19+
U: FromStr,
20+
<T as FromStr>::Err: std::fmt::Debug,
21+
<U as FromStr>::Err: std::fmt::Debug,
22+
{
23+
let s = self.as_ref();
24+
(s[0..pos].parse().unwrap(), s[pos..].parse().unwrap())
25+
}
26+
}

aoclp_solutions/src/y2025/day_01.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::str::FromStr;
33

44
use aoclp::anyhow::anyhow;
55
use aoclp::solvers_impl::input::safe_get_input_as_many;
6+
use aoclp::str::StrHelper;
67

78
pub fn part_1() -> usize {
89
moves().filter(|dial| *dial == 0).count()
@@ -81,8 +82,7 @@ impl FromStr for Rotation {
8182
type Err = aoclp::Error;
8283

8384
fn from_str(s: &str) -> Result<Self, Self::Err> {
84-
let direction: RotationDirection = s[0..1].parse()?;
85-
let clicks: i64 = s[1..].parse()?;
85+
let (direction, clicks) = s.split_parse_at(1);
8686
Ok(Self { direction, clicks })
8787
}
8888
}

0 commit comments

Comments
 (0)