day-1 refactored to be best practices with clippy
This commit is contained in:
@@ -9,7 +9,7 @@ use nom::{
|
|||||||
multi::separated_list1,
|
multi::separated_list1,
|
||||||
};
|
};
|
||||||
|
|
||||||
use error_stack::{Result, ResultExt, Context, Report};
|
use error_stack::{Context, Report, Result, ResultExt};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Day1Part1Error;
|
pub struct Day1Part1Error;
|
||||||
@@ -30,16 +30,21 @@ impl Display for Day1Part1Error{
|
|||||||
/// # Errors
|
/// # Errors
|
||||||
/// errors when can't parse the input
|
/// errors when can't parse the input
|
||||||
pub fn part1(input: &str) -> Result<String, Day1Part1Error> {
|
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:?}");
|
trace!("{values:?}");
|
||||||
values
|
values
|
||||||
.iter()
|
.iter()
|
||||||
.map(|v| {
|
.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 |{
|
.try_fold(0_u32, |sum, number| {
|
||||||
let Ok(sum) = sum else {return Err(Report::from(Day1Part1Error))};
|
let Ok(number) = number else {
|
||||||
let Ok(number) = number else { return Err(Report::from(Day1Part1Error))};
|
return Err(Report::from(Day1Part1Error));
|
||||||
|
};
|
||||||
Ok(sum + number)
|
Ok(sum + number)
|
||||||
})
|
})
|
||||||
.map(|x| x.to_string())
|
.map(|x| x.to_string())
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use std::{fmt::Display, ops::Not};
|
use std::{fmt::Display, ops::Not};
|
||||||
|
|
||||||
use error_stack::{Result, Context, Report};
|
use error_stack::{Context, Report, Result};
|
||||||
use log::trace;
|
use log::trace;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -24,16 +24,22 @@ impl Display for Day1Part2Error{
|
|||||||
/// # Errors
|
/// # Errors
|
||||||
/// this panics if there is no numbers in a line
|
/// this panics if there is no numbers in a line
|
||||||
pub fn part2(input: &str) -> Result<String, Day1Part2Error> {
|
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:?}");
|
trace!("{values:?}");
|
||||||
values
|
values
|
||||||
.iter()
|
.iter()
|
||||||
.map(|v| {
|
.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 |{
|
.try_fold(0_u32, |sum, number| {
|
||||||
let Ok(sum) = sum else {return Err(Report::from(Day1Part2Error))};
|
let Ok(number) = number else {
|
||||||
let Ok(number) = number else { return Err(Report::from(Day1Part2Error))};
|
return Err(Report::from(Day1Part2Error));
|
||||||
|
};
|
||||||
Ok(sum + number)
|
Ok(sum + number)
|
||||||
})
|
})
|
||||||
.map(|x| x.to_string())
|
.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") {
|
} else if reduced_line.starts_with("zero") {
|
||||||
Some(0)
|
Some(0)
|
||||||
} else {
|
} else {
|
||||||
reduced_line
|
reduced_line.chars().next().and_then(|x| x.to_digit(10))
|
||||||
.chars()
|
|
||||||
.next()
|
|
||||||
.and_then(|x| x.to_digit(10))
|
|
||||||
};
|
};
|
||||||
|
|
||||||
result
|
result
|
||||||
})
|
})
|
||||||
.collect();
|
.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)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user