From 01e069083b181551e33fd11057abda13b91ad999 Mon Sep 17 00:00:00 2001 From: Gabe Venberg Date: Mon, 4 Dec 2023 16:23:29 -0600 Subject: [PATCH] day 04 part 2 --- days/day04/src/main.rs | 2 +- days/day04/src/parse.rs | 30 +++++++++++++--------- days/day04/src/part1.rs | 12 ++++++--- days/day04/src/part2.rs | 56 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/days/day04/src/main.rs b/days/day04/src/main.rs index bafdbd2..456385c 100644 --- a/days/day04/src/main.rs +++ b/days/day04/src/main.rs @@ -10,5 +10,5 @@ fn main() { println!("Result: {}", part1::part1(&structured_input)); println!("Part Two"); - println!("Result: {}", part2::part2()); + println!("Result: {}", part2::part2(&structured_input)); } diff --git a/days/day04/src/parse.rs b/days/day04/src/parse.rs index 3e34b2b..42ceee1 100644 --- a/days/day04/src/parse.rs +++ b/days/day04/src/parse.rs @@ -7,11 +7,12 @@ use nom::{ IResult, }; -#[derive(Debug, PartialEq, Eq,)] +#[derive(Debug, PartialEq, Eq, Clone)] pub struct Card { pub id: usize, pub winning_numbers: BTreeSet, pub numbers: BTreeSet, + pub multiplier: usize, } impl Card { @@ -36,15 +37,13 @@ impl Card { id: id as usize, winning_numbers, numbers, + multiplier: 1, }, )) } - pub fn winning_number_matches(&self) -> BTreeSet { - self.numbers - .intersection(&self.winning_numbers) - .cloned() - .collect() + pub fn num_matches(&self) -> usize { + self.numbers.intersection(&self.winning_numbers).count() } } @@ -73,32 +72,38 @@ mod tests { Card { id: 1, winning_numbers: collection! {17, 41, 48, 83, 86}, - numbers: collection! {6, 9, 17, 31, 48, 53, 83, 86} + numbers: collection! {6, 9, 17, 31, 48, 53, 83, 86}, + multiplier: 1, }, Card { id: 2, winning_numbers: collection! {13, 16, 20, 32, 61}, - numbers: collection! {17, 19, 24, 30, 32, 61, 68, 82} + numbers: collection! {17, 19, 24, 30, 32, 61, 68, 82}, + multiplier: 1, }, Card { id: 3, winning_numbers: collection! {1, 21, 44, 53, 59}, - numbers: collection! {1, 14, 16, 21, 63, 69, 72, 82} + numbers: collection! {1, 14, 16, 21, 63, 69, 72, 82}, + multiplier: 1, }, Card { id: 4, winning_numbers: collection! {41, 69, 73, 84, 92}, - numbers: collection! {5, 51, 54, 58, 59, 76, 83, 84} + numbers: collection! {5, 51, 54, 58, 59, 76, 83, 84}, + multiplier: 1, }, Card { id: 5, winning_numbers: collection! {26, 28, 32, 83, 87}, - numbers: collection! {12, 22, 30, 36, 70, 82, 88, 93} + numbers: collection! {12, 22, 30, 36, 70, 82, 88, 93}, + multiplier: 1, }, Card { id: 6, winning_numbers: collection! {13 , 18, 31, 56, 72}, - numbers: collection! {10, 11, 23, 35, 36, 67, 74, 77} + numbers: collection! {10, 11, 23, 35, 36, 67, 74, 77}, + multiplier: 1, } ] ); @@ -115,6 +120,7 @@ mod tests { id: 1, winning_numbers: collection! {41, 48, 83, 86, 17}, numbers: collection! {83, 86, 6, 31, 17, 9, 48, 53}, + multiplier: 1, } ) ) diff --git a/days/day04/src/part1.rs b/days/day04/src/part1.rs index 8275cc6..3b00101 100644 --- a/days/day04/src/part1.rs +++ b/days/day04/src/part1.rs @@ -4,9 +4,9 @@ pub fn part1(input: &[Card]) -> usize { input .iter() .map(|c| { - let len = c.winning_number_matches().len(); - if len >=1{ - 2_usize.pow((len-1).try_into().unwrap()) + let len = c.num_matches(); + if len >= 1 { + 2_usize.pow((len - 1).try_into().unwrap()) } else { 0 } @@ -27,31 +27,37 @@ mod tests { id: 1, winning_numbers: collection! {17, 41, 48, 83, 86}, numbers: collection! {6, 9, 17, 31, 48, 53, 83, 86}, + multiplier: 1, }, Card { id: 2, winning_numbers: collection! {13, 16, 20, 32, 61}, numbers: collection! {17, 19, 24, 30, 32, 61, 68, 82}, + multiplier: 1, }, Card { id: 3, winning_numbers: collection! {1, 21, 44, 53, 59}, numbers: collection! {1, 14, 16, 21, 63, 69, 72, 82}, + multiplier: 1, }, Card { id: 4, winning_numbers: collection! {41, 69, 73, 84, 92}, numbers: collection! {5, 51, 54, 58, 59, 76, 83, 84}, + multiplier: 1, }, Card { id: 5, winning_numbers: collection! {26, 28, 32, 83, 87}, numbers: collection! {12, 22, 30, 36, 70, 82, 88, 93}, + multiplier: 1, }, Card { id: 6, winning_numbers: collection! {13 , 18, 31, 56, 72}, numbers: collection! {10, 11, 23, 35, 36, 67, 74, 77}, + multiplier: 1, }, ]; assert_eq!(part1(&input), 13); diff --git a/days/day04/src/part2.rs b/days/day04/src/part2.rs index 419e697..b45077d 100644 --- a/days/day04/src/part2.rs +++ b/days/day04/src/part2.rs @@ -1,15 +1,65 @@ use crate::parse::*; -pub fn part2() -> usize { - unimplemented!() +pub fn part2(input: &[Card]) -> usize { + let mut stack: Vec = input.to_vec(); + for i in 0..stack.len() { + let matches = stack[i].num_matches(); + if matches >= 1 { + //+1 because we want 3 matches to give the num 1,2,3 + for advance in 1..(matches+1){ + stack[i+advance].multiplier+=stack[i].multiplier + } + } + } + stack.iter().map(|c| c.multiplier).sum() } #[cfg(test)] mod tests { + use collection_literals::collection; + use super::*; #[test] fn test_part2() { - assert_eq!(0, 0); + let input = vec![ + Card { + id: 1, + winning_numbers: collection! {17, 41, 48, 83, 86}, + numbers: collection! {6, 9, 17, 31, 48, 53, 83, 86}, + multiplier: 1, + }, + Card { + id: 2, + winning_numbers: collection! {13, 16, 20, 32, 61}, + numbers: collection! {17, 19, 24, 30, 32, 61, 68, 82}, + multiplier: 1, + }, + Card { + id: 3, + winning_numbers: collection! {1, 21, 44, 53, 59}, + numbers: collection! {1, 14, 16, 21, 63, 69, 72, 82}, + multiplier: 1, + }, + Card { + id: 4, + winning_numbers: collection! {41, 69, 73, 84, 92}, + numbers: collection! {5, 51, 54, 58, 59, 76, 83, 84}, + multiplier: 1, + }, + Card { + id: 5, + winning_numbers: collection! {26, 28, 32, 83, 87}, + numbers: collection! {12, 22, 30, 36, 70, 82, 88, 93}, + multiplier: 1, + }, + Card { + id: 6, + winning_numbers: collection! {13 , 18, 31, 56, 72}, + numbers: collection! {10, 11, 23, 35, 36, 67, 74, 77}, + multiplier: 1, + }, + ]; + assert_eq!(part2(&input), 30); } }