day-1 refactored to be best practices with clippy

This commit is contained in:
Dylan Thies
2023-12-31 08:40:47 -05:00
parent 80b5ddd6d0
commit c45177590a
2 changed files with 33 additions and 21 deletions

View File

@@ -9,14 +9,14 @@ use nom::{
multi::separated_list1,
};
use error_stack::{Result, ResultExt, Context, Report};
use error_stack::{Context, Report, Result, ResultExt};
#[derive(Debug)]
pub struct Day1Part1Error;
impl Context for Day1Part1Error{}
impl Context for Day1Part1Error {}
impl Display for Day1Part1Error{
impl Display for Day1Part1Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "day 1 part 1 error")
}
@@ -30,16 +30,21 @@ impl Display for Day1Part1Error{
/// # Errors
/// errors when can't parse the input
pub fn part1(input: &str) -> Result<String, Day1Part1Error> {
let (_input, values) = parse_input(input).map_err(|x|Report::from(x.to_owned())).change_context(Day1Part1Error)?;
let (_input, values) = parse_input(input)
.map_err(|x| Report::from(x.to_owned()))
.change_context(Day1Part1Error)?;
trace!("{values:?}");
values
.iter()
.map(|v| {
v.first().and_then(|first| if let Some(last) = v.last() { Some(*first *10 + *last) } else {None}).ok_or(Day1Part1Error)
v.first()
.and_then(|first| v.last().map(|last| *first * 10 + *last))
.ok_or(Day1Part1Error)
})
.fold(Ok(0_u32), | sum, number |{
let Ok(sum) = sum else {return Err(Report::from(Day1Part1Error))};
let Ok(number) = number else { return Err(Report::from(Day1Part1Error))};
.try_fold(0_u32, |sum, number| {
let Ok(number) = number else {
return Err(Report::from(Day1Part1Error));
};
Ok(sum + number)
})
.map(|x| x.to_string())

View File

@@ -2,15 +2,15 @@
use std::{fmt::Display, ops::Not};
use error_stack::{Result, Context, Report};
use error_stack::{Context, Report, Result};
use log::trace;
#[derive(Debug)]
pub struct Day1Part2Error;
impl Context for Day1Part2Error{}
impl Context for Day1Part2Error {}
impl Display for Day1Part2Error{
impl Display for Day1Part2Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "day 1 part 2 error")
}
@@ -24,16 +24,22 @@ impl Display for Day1Part2Error{
/// # Errors
/// this panics if there is no numbers in a line
pub fn part2(input: &str) -> Result<String, Day1Part2Error> {
let values = input.lines().map(parse_line).collect::<Result<Vec<Vec<u32>>,_>>()?;
let values = input
.lines()
.map(parse_line)
.collect::<Result<Vec<Vec<u32>>, _>>()?;
trace!("{values:?}");
values
.iter()
.map(|v| {
v.first().and_then(|first| if let Some(last) = v.last() { Some(*first *10 + *last) } else {None}).ok_or(Day1Part2Error)
v.first()
.and_then(|first| v.last().map(|last| *first * 10 + *last))
.ok_or(Day1Part2Error)
})
.fold(Ok(0_u32), | sum, number |{
let Ok(sum) = sum else {return Err(Report::from(Day1Part2Error))};
let Ok(number) = number else { return Err(Report::from(Day1Part2Error))};
.try_fold(0_u32, |sum, number| {
let Ok(number) = number else {
return Err(Report::from(Day1Part2Error));
};
Ok(sum + number)
})
.map(|x| x.to_string())
@@ -64,16 +70,17 @@ fn parse_line(line: &str) -> Result<Vec<u32>, Day1Part2Error> {
} else if reduced_line.starts_with("zero") {
Some(0)
} else {
reduced_line
.chars()
.next()
.and_then(|x| x.to_digit(10))
reduced_line.chars().next().and_then(|x| x.to_digit(10))
};
result
})
.collect();
numbers.is_empty().not().then_some(numbers).ok_or(Report::from(Day1Part2Error))
numbers
.is_empty()
.not()
.then_some(numbers)
.ok_or(Report::from(Day1Part2Error))
}
#[cfg(test)]