switching 2023 day1 and day2 to this error and error-stack
This commit is contained in:
@@ -11,3 +11,8 @@ repository.workspace = true
|
||||
nom.workspace = true
|
||||
itertools.workspace = true
|
||||
log.workspace = true
|
||||
error-stack.workspace = true
|
||||
thiserror.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
test-log.workspace = true
|
||||
|
||||
@@ -3,10 +3,22 @@
|
||||
use day_2::part1;
|
||||
use day_2::part2;
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("./input.txt");
|
||||
let part1_result = part1(input);
|
||||
println!("part 1: {part1_result}");
|
||||
let part2_result = part2(input);
|
||||
println!("part 2: {part2_result}");
|
||||
use error_stack::{Result, ResultExt};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
enum Day2Error {
|
||||
#[error("Part 1 failed")]
|
||||
Part1Error,
|
||||
#[error("Part 2 failed")]
|
||||
Part2Error,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Day2Error> {
|
||||
let input = include_str!("./input.txt");
|
||||
let part1_result = part1(input).change_context(Day2Error::Part1Error)?;
|
||||
println!("part 1: {part1_result}");
|
||||
let part2_result = part2(input).change_context(Day2Error::Part2Error)?;
|
||||
println!("part 2: {part2_result}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
|
||||
use error_stack::{Report, Result, ResultExt};
|
||||
use log::debug;
|
||||
use nom::{
|
||||
bytes::complete::tag,
|
||||
@@ -7,6 +8,13 @@ use nom::{
|
||||
multi::separated_list1,
|
||||
sequence::{preceded, separated_pair},
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Day2Part1Error {
|
||||
#[error("there was a problem parsing")]
|
||||
ParseError,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Round {
|
||||
@@ -23,8 +31,7 @@ struct Game {
|
||||
|
||||
impl Game {
|
||||
fn to_part1(&self) -> Option<u32> {
|
||||
if self
|
||||
.rounds
|
||||
self.rounds
|
||||
.iter()
|
||||
.find_map(|r| {
|
||||
//TODO if inverted use find_map
|
||||
@@ -34,12 +41,8 @@ impl Game {
|
||||
None
|
||||
}
|
||||
})
|
||||
.is_some()
|
||||
{
|
||||
None
|
||||
} else {
|
||||
Some(self.id)
|
||||
}
|
||||
.is_none()
|
||||
.then_some(self.id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,16 +51,18 @@ impl Game {
|
||||
/// # Arguments
|
||||
/// - input the puszzle input
|
||||
///
|
||||
/// # Panics
|
||||
/// panics whenever the input isn't parsable
|
||||
pub fn part1(input: &str) -> String {
|
||||
let (_, games) = process_input(input).expect("there should be input");
|
||||
/// # Errors
|
||||
/// errors whenever the input isn't parsable
|
||||
pub fn part1(input: &'static str) -> Result<String, Day2Part1Error> {
|
||||
let (_, games) = process_input(input)
|
||||
.map_err(|err| Report::from(err.to_owned()))
|
||||
.change_context(Day2Part1Error::ParseError)?;
|
||||
debug!("{games:?}");
|
||||
games
|
||||
Ok(games
|
||||
.iter()
|
||||
.filter_map(Game::to_part1)
|
||||
.sum::<u32>()
|
||||
.to_string()
|
||||
.to_string())
|
||||
}
|
||||
|
||||
fn process_block(input: &str) -> nom::IResult<&str, (u32, String)> {
|
||||
@@ -107,9 +112,10 @@ Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";
|
||||
|
||||
#[test]
|
||||
#[test_log::test]
|
||||
#[test_log(default_log_filter = "trace")]
|
||||
fn part1_works() {
|
||||
let result = part1(INPUT);
|
||||
let result = part1(INPUT).unwrap();
|
||||
assert_eq!(result, "8".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
|
||||
use error_stack::{Report, Result, ResultExt};
|
||||
use nom::{
|
||||
bytes::complete::tag,
|
||||
character::complete::{self, newline},
|
||||
multi::separated_list1,
|
||||
sequence::{preceded, separated_pair},
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Day2Part2Error {
|
||||
#[error("there was a problem parsing")]
|
||||
ParseError,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Round {
|
||||
@@ -44,11 +52,13 @@ impl Game {
|
||||
/// # Arguments
|
||||
/// - input the puszzle input
|
||||
///
|
||||
/// # Panics
|
||||
/// panics whenever the input isn't parsable
|
||||
pub fn part2(input: &str) -> String {
|
||||
let (_, games) = process_input(input).expect("there should be input");
|
||||
games.iter().map(Game::to_power).sum::<u64>().to_string()
|
||||
/// # Errors
|
||||
/// errors whenever the input isn't parsable
|
||||
pub fn part2(input: &str) -> Result<String, Day2Part2Error> {
|
||||
let (_, games) = process_input(input)
|
||||
.map_err(|err| Report::from(err.to_owned()))
|
||||
.change_context(Day2Part2Error::ParseError)?; //expect("there should be input");
|
||||
Ok(games.iter().map(Game::to_power).sum::<u64>().to_string())
|
||||
}
|
||||
|
||||
fn process_block(input: &str) -> nom::IResult<&str, (u32, String)> {
|
||||
@@ -98,9 +108,10 @@ Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";
|
||||
|
||||
#[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, "2286".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user