Finish part-1

This commit is contained in:
Kristofers Solo 2025-12-01 19:16:34 +02:00
parent 2d34e14f7d
commit 1dcdb86bed
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
3 changed files with 94 additions and 5 deletions

View File

@ -1,6 +1,8 @@
use miette::Result; use miette::Result;
#[tracing::instrument] #[tracing::instrument]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::missing_errors_doc)]
pub fn process(input: &str) -> Result<usize> { pub fn process(input: &str) -> Result<usize> {
todo!("day xx - part 1"); todo!("day xx - part 1");
Ok(0) Ok(0)

View File

@ -1,6 +1,8 @@
use miette::Result; use miette::Result;
#[tracing::instrument] #[tracing::instrument]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::missing_errors_doc)]
pub fn process(input: &str) -> Result<usize> { pub fn process(input: &str) -> Result<usize> {
todo!("day xx - part 2"); todo!("day xx - part 2");
Ok(0) Ok(0)

View File

@ -1,9 +1,85 @@
use miette::Result; use miette::Result;
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq)]
enum Direction {
Left,
Right,
}
impl FromStr for Direction {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.to_lowercase().trim() {
"l" => Ok(Self::Left),
"r" => Ok(Self::Right),
_ => Err("Wrong value".to_string()),
}
}
}
#[derive(Debug, Clone, Copy)]
struct Rotation {
amount: i32,
direction: Direction,
}
impl Rotation {
const fn right(self, dial: i32) -> i32 {
(dial + self.amount).rem_euclid(100)
}
const fn left(self, dial: i32) -> i32 {
(dial - self.amount).rem_euclid(100)
}
}
impl FromStr for Rotation {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let trimmed = s.trim();
let (direction_str, amount_str) = trimmed.split_at(1);
let direction = direction_str.parse()?;
let amount = amount_str.parse::<i32>().map_err(|e| e.to_string())?;
Ok(Self { amount, direction })
}
}
#[derive(Debug, Clone)]
struct Sequence {
rotations: Vec<Rotation>,
}
impl FromStr for Sequence {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let rotations = s
.trim()
.lines()
.map(|line| line.parse::<Rotation>().unwrap())
.collect::<Vec<_>>();
Ok(Self { rotations })
}
}
#[tracing::instrument] #[tracing::instrument]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::missing_errors_doc)]
pub fn process(input: &str) -> Result<usize> { pub fn process(input: &str) -> Result<usize> {
todo!("day xx - part 1"); let sequence = input.parse::<Sequence>().unwrap();
Ok(0) let mut count = 0;
sequence.rotations.iter().fold(50, |acc, rotation| {
let number = match rotation.direction {
Direction::Left => rotation.left(acc),
Direction::Right => rotation.right(acc),
};
if number == 0 {
count += 1;
}
number
});
Ok(count)
} }
#[cfg(test)] #[cfg(test)]
@ -12,9 +88,18 @@ mod tests {
#[test] #[test]
fn test_process() -> Result<()> { fn test_process() -> Result<()> {
let input = ""; let input = "L68
todo!("haven't built test yet"); L30
let result = 0; R48
L5
R60
L55
L1
L99
R14
L82
";
let result = 3;
assert_eq!(process(input)?, result); assert_eq!(process(input)?, result);
Ok(()) Ok(())
} }