diff --git a/day_02/src/lib.rs b/day_02/src/lib.rs index e82bc3f..844a0db 100644 --- a/day_02/src/lib.rs +++ b/day_02/src/lib.rs @@ -15,7 +15,12 @@ mod tests { use super::*; #[test] - fn example_score_is_correct() { + fn part_01_example_score_is_correct() { assert_eq!(15, run_day_and_part(Day::Two, Part::One, "A Y\nB X\nC Z")) } + + #[test] + fn part_02_example_score_is_correct() { + assert_eq!(12, run_day_and_part(Day::Two, Part::Two, "A Y\nB X\nC Z")) + } } diff --git a/day_02/src/parser.rs b/day_02/src/parser.rs index 320d702..1b7163a 100644 --- a/day_02/src/parser.rs +++ b/day_02/src/parser.rs @@ -160,8 +160,10 @@ pub(crate) mod part_02 { } pub(crate) fn parse(inp: &str) -> Vec { - todo!() + let (_, tricks): (&str, Vec) = sequence_parser(inp).expect("Couldn't parse input"); + tricks.iter().map(|trick| trick.score()).collect() } + #[cfg(test)] mod tests { use super::*; @@ -181,8 +183,8 @@ pub(crate) mod part_02 { } macro_rules! param_test { - ($name:ident, $input:literal, $against:expr, $result:expr, $thrown:expr) => { - #[test] + ($($name:ident: $input:literal , $against:expr , $result:expr , $thrown:expr;)*) => { + $(#[test] fn $name() { assert_eq!( Ok(( @@ -194,17 +196,43 @@ pub(crate) mod part_02 { )), trick_parser($input) ) - } + })* }; } param_test!( - rock_thrown_loss_expected, - "A X", - Hand::Rock, - ResultExpected::Lose, - Hand::Scissors + rock_thrown_loss_expected: "A X" , Hand::Rock , ResultExpected::Lose , Hand::Scissors; + rock_thrown_draw_expected: "A Y", Hand::Rock, ResultExpected::Draw, Hand::Rock; + rock_thrown_win_expected: "A Z", Hand::Rock, ResultExpected::Win, Hand::Paper; + + paper_thrown_loss_expected: "B X" , Hand::Paper , ResultExpected::Lose , Hand::Rock; + paper_thrown_draw_expected: "B Y", Hand::Paper, ResultExpected::Draw, Hand::Paper; + paper_thrown_win_expected: "B Z", Hand::Paper, ResultExpected::Win, Hand::Scissors; + + scissors_thrown_loss_expected: "C X" , Hand::Scissors , ResultExpected::Lose , Hand::Paper; + scissors_thrown_draw_expected: "C Y", Hand::Scissors, ResultExpected::Draw, Hand::Scissors; + scissors_thrown_win_expected: "C Z", Hand::Scissors, ResultExpected::Win, Hand::Rock; ); + + #[test] + fn example_sequence_parsed() { + assert_eq!( + Ok(("", vec![ + Trick { + against: Hand::Rock, + thrown: Hand::Rock, + }, + Trick { + against: Hand::Paper, + thrown: Hand::Rock, + }, + Trick { + against: Hand::Scissors, + thrown: Hand::Rock, + } + ])), sequence_parser("A Y\nB X\nC Z") + ) + } } }