attempting to best practice libraries for day 1

This commit is contained in:
Dylan Thies
2023-12-31 08:34:57 -05:00
parent 9a69cced80
commit 844419faf5
4 changed files with 74 additions and 36 deletions

View File

@@ -13,4 +13,8 @@ derive-getters.workspace = true
error-stack.workspace = true error-stack.workspace = true
itertools.workspace = true itertools.workspace = true
log.workspace = true log.workspace = true
logs = "0.7.1"
nom.workspace = true nom.workspace = true
[dev-dependencies]
test-log = {version="0.2.14", features=["default", "unstable"]}

View File

@@ -5,8 +5,8 @@ use day_1::part2::part2;
fn main() { fn main() {
let input = include_str!("./input.txt"); let input = include_str!("./input.txt");
let (_, part1_result) = part1(input).unwrap(); let part1_result = part1(input).unwrap();
println!("part 1: {part1_result}"); println!("part 1: {part1_result}");
let part2_result = part2(input); let part2_result = part2(input).unwrap();
println!("part 2: {part2_result}"); println!("part 2: {part2_result}");
} }

View File

@@ -1,35 +1,48 @@
#![warn(clippy::all, clippy::pedantic)] #![warn(clippy::all, clippy::pedantic)]
use std::fmt::Display;
use log::trace;
use nom::{ use nom::{
self, self,
character::complete::{alphanumeric1, newline}, character::complete::{alphanumeric1, newline},
multi::separated_list1, multi::separated_list1,
}; };
use error_stack::{Result, ResultExt, Context, Report};
#[derive(Debug)]
pub struct Day1Part1Error;
impl Context for Day1Part1Error{}
impl Display for Day1Part1Error{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "day 1 part 1 error")
}
}
/// Day-1 part 1 of AC2023 /// Day-1 part 1 of AC2023
/// ///
/// # Arguments /// # Arguments
/// - input the input for day1 as a string /// - input the input for day1 as a string
/// ///
/// # Panics
/// This panics whenever a number isn't present in a line of the input
///
/// # Errors /// # Errors
/// errors when can't parse the input /// errors when can't parse the input
pub fn part1(input: &str) -> nom::IResult<&str, String> { pub fn part1(input: &str) -> Result<String, Day1Part1Error> {
let (_, values) = parse_input(input)?; let (_input, values) = parse_input(input).map_err(|x|Report::from(x.to_owned())).change_context(Day1Part1Error)?;
println!("{values:?}"); trace!("{values:?}");
Ok(( values
"", .iter()
values .map(|v| {
.iter() v.first().and_then(|first| if let Some(last) = v.last() { Some(*first *10 + *last) } else {None}).ok_or(Day1Part1Error)
.map(|v| { })
v.first().expect("always at least one number") * 10 .fold(Ok(0_u32), | sum, number |{
+ v.last().expect("always atleast one number") let Ok(sum) = sum else {return Err(Report::from(Day1Part1Error))};
}) let Ok(number) = number else { return Err(Report::from(Day1Part1Error))};
.sum::<u32>() Ok(sum + number)
.to_string(), })
)) .map(|x| x.to_string())
} }
fn parse_input(input: &str) -> nom::IResult<&str, Vec<Vec<u32>>> { fn parse_input(input: &str) -> nom::IResult<&str, Vec<Vec<u32>>> {
@@ -54,9 +67,10 @@ pqr3stu8vwx
a1b2c3d4e5f a1b2c3d4e5f
treb7uchet"; treb7uchet";
#[test] #[test_log::test]
#[test_log(default_log_filter = "trace")]
fn part1_works() { fn part1_works() {
let (_, result) = part1(INPUT).unwrap(); let result = part1(INPUT).unwrap();
assert_eq!(result, "142".to_string()); assert_eq!(result, "142".to_string());
} }
} }

View File

@@ -1,27 +1,46 @@
#![warn(clippy::all, clippy::pedantic)] #![warn(clippy::all, clippy::pedantic)]
use std::{fmt::Display, ops::Not};
use error_stack::{Result, Context, Report};
use log::trace;
#[derive(Debug)]
pub struct Day1Part2Error;
impl Context for Day1Part2Error{}
impl Display for Day1Part2Error{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "day 1 part 2 error")
}
}
/// Day 1 Part 2 of AOC2023 /// Day 1 Part 2 of AOC2023
/// ///
/// # Arguments /// # Arguments
/// - puzzle input /// - puzzle input
/// ///
/// # Panics /// # 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) -> String { pub fn part2(input: &str) -> Result<String, Day1Part2Error> {
let values = input.lines().map(parse_line).collect::<Vec<Vec<u32>>>(); let values = input.lines().map(parse_line).collect::<Result<Vec<Vec<u32>>,_>>()?;
println!("{values:?}"); trace!("{values:?}");
values values
.iter() .iter()
.map(|v| { .map(|v| {
v.first().expect("There is always at least one number") * 10 v.first().and_then(|first| if let Some(last) = v.last() { Some(*first *10 + *last) } else {None}).ok_or(Day1Part2Error)
+ v.last().expect("there is always at least one number")
}) })
.sum::<u32>() .fold(Ok(0_u32), | sum, number |{
.to_string() let Ok(sum) = sum else {return Err(Report::from(Day1Part2Error))};
let Ok(number) = number else { return Err(Report::from(Day1Part2Error))};
Ok(sum + number)
})
.map(|x| x.to_string())
} }
fn parse_line(line: &str) -> Vec<u32> { fn parse_line(line: &str) -> Result<Vec<u32>, Day1Part2Error> {
(0..line.len()) let numbers: Vec<u32> = (0..line.len())
.filter_map(|index| { .filter_map(|index| {
let reduced_line = &line[index..]; let reduced_line = &line[index..];
let result = if reduced_line.starts_with("one") { let result = if reduced_line.starts_with("one") {
@@ -48,13 +67,13 @@ fn parse_line(line: &str) -> Vec<u32> {
reduced_line reduced_line
.chars() .chars()
.next() .next()
.expect("there is alwayss a character") .and_then(|x| x.to_digit(10))
.to_digit(10)
}; };
result result
}) })
.collect() .collect();
numbers.is_empty().not().then_some(numbers).ok_or(Report::from(Day1Part2Error))
} }
#[cfg(test)] #[cfg(test)]
@@ -69,9 +88,10 @@ xtwone3four
zoneight234 zoneight234
7pqrstsixteen"; 7pqrstsixteen";
#[test] #[test_log::test]
#[test_log(default_log_filter = "trace")]
fn part2_works() { fn part2_works() {
let result = part2(INPUT); let result = part2(INPUT).unwrap();
assert_eq!(result, "281".to_string()); assert_eq!(result, "281".to_string());
} }
} }