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

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

View File

@@ -1,35 +1,48 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fmt::Display;
use log::trace;
use nom::{
self,
character::complete::{alphanumeric1, newline},
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
///
/// # Arguments
/// - 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 when can't parse the input
pub fn part1(input: &str) -> nom::IResult<&str, String> {
let (_, values) = parse_input(input)?;
println!("{values:?}");
Ok((
"",
values
.iter()
.map(|v| {
v.first().expect("always at least one number") * 10
+ v.last().expect("always atleast one number")
})
.sum::<u32>()
.to_string(),
))
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)?;
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)
})
.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))};
Ok(sum + number)
})
.map(|x| x.to_string())
}
fn parse_input(input: &str) -> nom::IResult<&str, Vec<Vec<u32>>> {
@@ -54,9 +67,10 @@ pqr3stu8vwx
a1b2c3d4e5f
treb7uchet";
#[test]
#[test_log::test]
#[test_log(default_log_filter = "trace")]
fn part1_works() {
let (_, result) = part1(INPUT).unwrap();
let result = part1(INPUT).unwrap();
assert_eq!(result, "142".to_string());
}
}

View File

@@ -1,27 +1,46 @@
#![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
///
/// # Arguments
/// - puzzle input
///
/// # Panics
/// # Errors
/// this panics if there is no numbers in a line
pub fn part2(input: &str) -> String {
let values = input.lines().map(parse_line).collect::<Vec<Vec<u32>>>();
println!("{values:?}");
pub fn part2(input: &str) -> Result<String, Day1Part2Error> {
let values = input.lines().map(parse_line).collect::<Result<Vec<Vec<u32>>,_>>()?;
trace!("{values:?}");
values
.iter()
.map(|v| {
v.first().expect("There is always at least one number") * 10
+ v.last().expect("there is always at least one number")
v.first().and_then(|first| if let Some(last) = v.last() { Some(*first *10 + *last) } else {None}).ok_or(Day1Part2Error)
})
.sum::<u32>()
.to_string()
.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))};
Ok(sum + number)
})
.map(|x| x.to_string())
}
fn parse_line(line: &str) -> Vec<u32> {
(0..line.len())
fn parse_line(line: &str) -> Result<Vec<u32>, Day1Part2Error> {
let numbers: Vec<u32> = (0..line.len())
.filter_map(|index| {
let reduced_line = &line[index..];
let result = if reduced_line.starts_with("one") {
@@ -48,13 +67,13 @@ fn parse_line(line: &str) -> Vec<u32> {
reduced_line
.chars()
.next()
.expect("there is alwayss a character")
.to_digit(10)
.and_then(|x| x.to_digit(10))
};
result
})
.collect()
.collect();
numbers.is_empty().not().then_some(numbers).ok_or(Report::from(Day1Part2Error))
}
#[cfg(test)]
@@ -69,9 +88,10 @@ xtwone3four
zoneight234
7pqrstsixteen";
#[test]
#[test_log::test]
#[test_log(default_log_filter = "trace")]
fn part2_works() {
let result = part2(INPUT);
let result = part2(INPUT).unwrap();
assert_eq!(result, "281".to_string());
}
}