From e0d5fd7b93044eb200a8238b9090b156dfd28b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Fri, 8 Dec 2023 09:09:32 +0100 Subject: [PATCH] feat: add day 4 - part 2 --- day_4/README.md | 35 +++++++++++++++++++++++ day_4/src/lib.rs | 71 ++++++++++++++++++++++++++++++++++------------- day_4/src/main.rs | 4 +-- 3 files changed, 88 insertions(+), 22 deletions(-) diff --git a/day_4/README.md b/day_4/README.md index 86e0cc0..7d00eb6 100644 --- a/day_4/README.md +++ b/day_4/README.md @@ -48,3 +48,38 @@ Your puzzle answer was `24160`. **The first half of this puzzle is complete! It provides one gold star: `*`.** ## Instructions - Part 2 + +Just as you're about to report your findings to the Elf, one of you realizes that the rules have actually been printed on the back of every card this whole time. + +There's no such thing as "points". Instead, scratchcards only cause you to **win more scratchcards** equal to the number of winning numbers you have. + +Specifically, you win **copies** of the scratchcards below the winning card equal to the number of matches. So, if card 10 were to have 5 matching numbers, you would win one copy each of cards 11, 12, 13, 14, and 15. + +Copies of scratchcards are scored like normal scratchcards and have the **same card number** as the card they copied. So, if you win a copy of card 10 and it has 5 matching numbers, it would then win a copy of the same cards that the original card 10 won: cards 11, 12, 13, 14, and 15. This process repeats until none of the copies cause you to win any more cards. (Cards will never make you copy a card past the end of the table.) + +This time, the above example goes differently: + +```txt +Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 +Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 +Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 +Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 +Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 +Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 +``` + +- Card 1 has four matching numbers, so you win one copy each of the next four cards: cards 2, 3, 4, and 5. +- Your original card 2 has two matching numbers, so you win one copy each of cards 3 and 4. +- Your copy of card 2 also wins one copy each of cards 3 and 4. +- Your four instances of card 3 (one original and three copies) have two matching numbers, so you win **four** copies each of cards 4 and 5. +- Your eight instances of card 4 (one original and seven copies) have one matching number, so you win **eight** copies of card 5. +- Your fourteen instances of card 5 (one original and thirteen copies) have no matching numbers and win no more cards. +- Your one instance of card 6 (one original) has no matching numbers and wins no more cards. + +Once all of the originals and copies have been processed, you end up with 1 instance of card 1, **`2`** instances of card 2, **`4`** instances of card 3, **`8`** instances of card 4, **`14`** instances of card 5, and **`1`** instance of card 6. In total, this example pile of scratchcards causes you to ultimately have **`30`** scratchcards! + +Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of scratchcards, **how many total scratchcards do you end up with**? + +Your puzzle answer was `5659035`. + +**Both parts of this puzzle are complete! They provide two gold stars: `**`.** diff --git a/day_4/src/lib.rs b/day_4/src/lib.rs index f2fe731..f88cde0 100644 --- a/day_4/src/lib.rs +++ b/day_4/src/lib.rs @@ -1,6 +1,7 @@ +use std::cmp; use std::str::FromStr; -#[derive(Debug, Default, PartialEq)] +#[derive(Debug, Default, PartialEq, Clone)] pub struct CardNumbers { pub numbers: Vec, } @@ -41,11 +42,10 @@ impl FromStr for CardNumbers { } } -#[derive(Debug, Default, PartialEq)] +#[derive(Debug, Default, PartialEq, Clone)] pub struct Card { pub id: usize, - pub winning_numbers: CardNumbers, - pub owned_numbers: CardNumbers, + pub winning_numbers_count: usize, } impl FromStr for Card { @@ -66,8 +66,7 @@ impl FromStr for Card { /// let string = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53"; /// let expected_result = Card { /// id: 1, - /// winning_numbers: CardNumbers { numbers: vec![41, 48, 83, 86, 17], }, - /// owned_numbers: CardNumbers { numbers: vec![83, 86, 6, 31, 17, 9, 48, 53], }, + /// winning_numbers_count: 4, /// }; /// let actual_result = Card::from_str(string).unwrap(); /// @@ -81,16 +80,23 @@ impl FromStr for Card { .unwrap_or("Card 1") .strip_prefix("Card ") .unwrap_or("1") + .trim() .parse() .unwrap_or(1); let mut numbers_parts = parts.next().unwrap_or("").split(" | "); - result.winning_numbers = + let winning_numbers = CardNumbers::from_str(numbers_parts.next().unwrap_or("")).unwrap_or_default(); - result.owned_numbers = + let owned_numbers = CardNumbers::from_str(numbers_parts.next().unwrap_or("")).unwrap_or_default(); + result.winning_numbers_count = owned_numbers + .numbers + .iter() + .filter(|&owned_number| winning_numbers.numbers.contains(owned_number)) + .count(); + Ok(result) } } @@ -100,15 +106,10 @@ pub fn part_1(input: &str) -> usize { .lines() .map(|line| Card::from_str(line).unwrap_or_default()) .map(|card| { - let numbers_matches_count = card - .owned_numbers - .numbers - .iter() - .filter(|&owned_number| card.winning_numbers.numbers.contains(owned_number)) - .count() as u32; + let winning_numbers_count = card.winning_numbers_count as u32; let base: usize = 2; - if numbers_matches_count > 0 { - base.pow(numbers_matches_count.saturating_sub(1)) + if winning_numbers_count > 0 { + base.pow(winning_numbers_count.saturating_sub(1)) } else { 0 } @@ -116,6 +117,36 @@ pub fn part_1(input: &str) -> usize { .sum::() } +pub fn part_2(input: &str) -> usize { + let cards = input + .lines() + .map(|line| Card::from_str(line).unwrap_or_default()) + .collect::>(); + let mut cards_identifiers = cards.iter().map(|card| card.id).collect::>(); + + let mut index_card = 0; + loop { + if index_card >= cards_identifiers.len() { + break; + } + + if let Some(card) = cards.get(cards_identifiers.get(index_card).unwrap_or(&0) - 1) { + let card_position_index = card.id - 1; + let maximum_index = cmp::min( + card_position_index + card.winning_numbers_count, + cards.len() - 1, + ); + for index in card_position_index + 1..=maximum_index { + cards_identifiers.push(index + 1); + } + } + + index_card += 1; + } + + cards_identifiers.len() +} + #[cfg(test)] mod day_4_tests { use super::*; @@ -125,8 +156,8 @@ mod day_4_tests { assert_eq!(part_1(include_str!("../input_example_1.txt")), 13); } - // #[test] - // fn test_part_2_example() { - // assert_eq!(part_2(include_str!("../input_example_1.txt")), 467835); - // } + #[test] + fn test_part_2_example() { + assert_eq!(part_2(include_str!("../input_example_1.txt")), 30); + } } diff --git a/day_4/src/main.rs b/day_4/src/main.rs index 350f51b..9a71dc5 100644 --- a/day_4/src/main.rs +++ b/day_4/src/main.rs @@ -1,8 +1,8 @@ -use day_4::part_1; +use day_4::{part_1, part_2}; fn main() { let input = include_str!("../input.txt"); println!("- Day 4: Scratchcards -"); println!("Answer Part 1: {}", part_1(input)); - // println!("Answer Part 2: {}", part_2(input)); + println!("Answer Part 2: {}", part_2(input)); }